如何修改正则表达式以在特定字符串和字符之间进行搜索?

huangapple go评论58阅读模式
英文:

How to modify a regular expression to search between a special string and a character?

问题

我正在尝试编写一个正则表达式来搜索特定字符串和字符之间的内容。

我有一个正则表达式:

b(\(.*?)\)

它用于查找在 "b(" 和 ")" 之间的匹配项,例如:
字符串:az[b('0x611', '0*L3')]
匹配:b('0x611', '0*L3')

但有时会出现这样的行:
字符串:(G, f[b('0x4ed', 'S)Oi')])

在这种情况下,匹配项将是:b('0x4ed', 'S), 这是错误的。

我尝试通过查找在 "b('0x4ed', 'S))" 和 ")" 之间的匹配项来解决这个问题,但当我尝试将正则表达式改成如下形式 b('0x4ed', 'S)(.*?)\), 它就不起作用了。

我该如何解决这个问题?

英文:

I'm trying to write a regular expression to search between a special string and a character.

I have a regular expression:

b(\(.*?)\)

It looks for matches between "b(" and ")", for example:
String: az[b('0x611', '0*L3')]
Match: b('0x611', '0*L3')

But sometimes there are lines like this:
String: (G, f[b('0x4ed', 'S)Oi')])

And in this case match will be: b('0x4ed', 'S), and that wrong.

I'm trying to solve this by looking for matches between "b('0x4ed', 'S)" and ")", but when I try to bring the regular expression to the following form b('0x4ed', 'S)(\(.*?)\), it stops working.

How could I get out of this situation?

答案1

得分: 1

Use: "not one or more singlequotes until a singlequote" like: '[^']+'

Example

b\('[^']+', ?'[^']+'\)

Demo on Regex101.com

PS: if the value can be empty '' use zero-or-more *:

b\('[^']+', ?'[^']*'\)
英文:

Use: "not one or more singlequotes until a singlequote" like: '[^']+'

Example

b\('[^']+', ?'[^']+'\)

Demo on Regex101.com

PS: if the value can be empty '' use zero-or-more *:

b\('[^']+', ?'[^']*'\)

huangapple
  • 本文由 发表于 2023年5月28日 17:32:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定