gpt4 book ai didi

regex - 如何使用正则表达式模式提取括号中的子字符串

转载 作者:行者123 更新时间:2023-12-04 01:33:58 25 4
gpt4 key购买 nike

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

说,我有以下几行:

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

我将不得不寻找这种模式:
" (<any string>)

为了检索:
B. Wilson/Asher/Love

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

最佳答案

编辑 : 检查您的文档后,问题是括号前有不间断空格,而不是常规空格。所以这个正则表达式应该可以工作:""[ \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

关于regex - 如何使用正则表达式模式提取括号中的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10903394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com