Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "?:" do in regex?

Tags:

regex

I wanted to match anything but a string using regex. I did some Googling and found this: ^(?:(?!test).)* What do ?: and ?! do? Thanks.

like image 732
Leo Jiang Avatar asked Jun 30 '26 11:06

Leo Jiang


1 Answers

(?:) is non-capturing. That means that a match occurs as usual, but the parentheses are only for grouping (in this case to attach a * operator to the entire thing); the matched value cannot be pulled out later with $1 or \1.

(?!) is a negative lookahead assertion. That means that it matches if the string in the parentheses does not exist there.

See http://docs.python.org/library/re.html for some more operators. While regex varies in different languages, they're fairly similar.

like image 124
Dave Avatar answered Jul 03 '26 04:07

Dave