简体   繁体   English

正则表达式:模式包含0个或多个特殊字符

[英]Regex: pattern contains 0 or more special characters

this syntax returns an array with a word key. 此语法返回带有单词键的数组。

(?<item>\w+)  

ex. 例如 $array['item'] $ array ['item']



I want the return value in this form but I want to search for a word that may or may not contain parentheses (with values in between). 我想要这种形式的返回值, 我想搜索一个可能包含或不包含括号(中间含值)的单词。

"hello" “你好”
or 要么
"hello(some text and line breaks)" “您好(某些文本和换行符)”



What is the syntax for this? 这是什么语法?

\w+(?:\([^)]*\))?

will match words that can optionally be directly followed by a parenthesized text. 将匹配可以选择在其后直接带有括号的文本的单词。 Nested parentheses are not allowed. 不允许使用括号。

\w+    # Match alnum characters
(?:    # Match the following (non-capturing) group:
 \(    # literal (
 [^)]* # any number of characters except )
 \)    # literal )
)?     # End of group; make it optional
hello(\(\))?

或如果您的正则表达式引擎默认括号为普通字符

hello\(()\)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM