Zebra
Zebra

Reputation: 4006

Regex: pattern contains 0 or more special characters

this syntax returns an array with a word key.

(?<item>\w+)  

ex. $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?

Upvotes: 0

Views: 232

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336158

\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

Upvotes: 2

Robokop
Robokop

Reputation: 926

hello(\(\))?

or if your regex engine defaults parentheses to normal character

hello\(()\)?

Upvotes: 0

Related Questions