For example I have string:
"one two (78-45ack sack); now (87 back sack) follow dollow (59 uhhaaa)"
and I need only whole substring with parenthesis, containing word "back"
, for this string it will be:
"(87 back sack)"
I've tried:
(\(.*?back.*?\))
but it's return "(78-45ack sack); now (87 back sack)"
How should my regex look like? As I understood it's happening cause search going from begin of the string, in common - how to perform regex to "search"
from the middle of string, from the word "back"
?
You can use this regex based on negated character class:
[^()]*
matches 0 or more of any character that is not(
and)
, thus making sure we don't match across the pair of(...)
.RegEx Demo 1
Alternatively, you can also use this regex based on negative regex:
RegEx Demo 2
(?!back|[()])
is negative lookahead that asserts that current position doesn't haveback
or(
or)
ahead in the text.(?:(?!back|\)).)*?
matches 0 or more of any character that doesn't haveback
or(
or)
ahead.[^)]*
matches anything but)
.Try the below pattern for reluctant matching
pattern="\(.*?\)"