Reputation: 73
I have a string '(test)skip(test774)2;3(xx2324)'
. The presence of the word 'skip'
is necessary.
re.search('skip[(].+[)]', args[0]).group(0)
gives me 'skip(test774)'
, but not 'test774'
. I can could use slicing [5:-1]
to get 'test774'
, but it's too clumsy
What the easiest way to get only the value in brackets ('test774') using regular expressions?
Upvotes: 0
Views: 49
Reputation: 6281
To isolate the text you want, use a capturing group around the part of the regex that matches that text.
re.search('skip[(]([^)]+)[)]', args[0]).group(1)
The change I made is where you have .+
(any non-empty sequence of characters), I have ([^)]+)
(a capturing group containing a non-empty sequence of characters other than )
; this means this part of the match stops just before the first )
, rather than just before the last )
, as .+
would).
regex101.com demo and explanation
Upvotes: 2