如何使用正则表达式模式提取括号中的子字符串 [英] How to extract substring in parentheses using Regex pattern

查看:81
本文介绍了如何使用正则表达式模式提取括号中的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个简单的问题,但不幸的是我无法得到我想要的结果......

This is probably a simple problem, but unfortunately I wasn't able to get the results I wanted...

比如说,我有以下几行:

Say, I have the following line:

"Wouldn't It Be Nice" (B. Wilson/Asher/Love)

我必须寻找这种模式:

" (<any string>)

为了检索:

B. Wilson/Asher/Love

我尝试过类似 "" (([^))]*)) 之类的东西,但它似乎不起作用.另外,我想使用 Match.Submatches(0) 这样可能会使事情复杂化,因为它依赖于括号...

I tried something like "" (([^))]*)) but it doesn't seem to work. Also, I'd like to use Match.Submatches(0) so that might complicate things a bit because it relies on brackets...

推荐答案

编辑:检查您的文档后,问题是括号前有不间断空格,而不是常规空格.所以这个正则表达式应该可以工作: ""[ \xA0]*\(([^)]+)\)

Edit: After examining your document, the problem is that there are non-breaking spaces before the parentheses, not regular spaces. So this regex should work: ""[ \xA0]*\(([^)]+)\)

""       'quote (twice to escape)
[ \xA0]* 'zero or more non-breaking (\xA0) or a regular spaces
\(       'left parenthesis
(        'open capturing group
[^)]+    'anything not a right parenthesis
)        'close capturing group
\)       'right parenthesis

在函数中:

Public Function GetStringInParens(search_str As String)
Dim regEx As New VBScript_RegExp_55.RegExp
Dim matches
    GetStringInParens = ""
    regEx.Pattern = """[ \xA0]*\(([^)]+)\)"
    regEx.Global = True
    If regEx.test(search_str) Then
        Set matches = regEx.Execute(search_str)
        GetStringInParens = matches(0).SubMatches(0)
    End If
End Function

这篇关于如何使用正则表达式模式提取括号中的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆