正则表达式从括号内返回文本 [英] Regex pattern to return text from within parenthesis
问题描述
我要寻找一个正则表达式模式将返回我的第一套括号中的内容的字符串。
I am looking for a regex pattern that will return me the contents of the first set of parenthesis in a string.
例如,
text text text text (hello) text (hello2) (hello3) text
将返回你好
有谁知道什么图案看起来像C#?
Does anyone know what the pattern looks like for c#?
推荐答案
的正则表达式模式将是这个样子:
The regexp pattern would look something like this:
\(([^)]*)\)
模式尸检:
\\(
- 字符(
(
- 子模式的启动:
(
- start of subpattern:
[^)] *
匹配0个或更多字符,这不是)
- 注:我们'重新定义字符组,所以我们没有在这里躲避)
字符。
[^)]*
match 0 or more characters which are not ")
" - note: we're defining a character group, so we don't have to escape the )
character here.
)
- 子模式的结束
)
- end of subpattern
\\)
- 字符)
完整的模式将匹配支架和内他们的文本中,第一个子模式将只匹配括号内的文本(见C#参考如何让他们 - 我不说话C#;))
The full pattern will match the brackets and the text inside them, the first subpattern will match only the text inside the brackets (see C# reference how to get them - I don't speak C# ;))
这篇关于正则表达式从括号内返回文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!