Meaning of ?: ?= ?! ? <= ? <! in a regex

Asked

Viewed 6,538 times

18

In several regex I noticed some symbols that do not seem to be part of the capture but some kind of functionality. I would like to know the name or term of these symbols and what is the functionality of each one.

?:
?=
?!
?<=
?<!

2 answers

19


If you are referring to . Net regexes, using the class Regex, these symbols can be used when starting a group with parentheses:

( + symbols + ... + )

What do they mean:

  • ?: Undetected group: indicates a group that will not be in the list of captured groups... note that this will be considered normally within the match, only it will not be a group, for example:

    Parsed string: abc. 123 xpto<fim>
    Regex: \w+(?:\.|<fim>)
    Matches: abc., xpto<fim>

The others are assertive, without capture, nor do they advance in reading:

  • ?= Lookahead positive: This is an assertive, which checks whether the group can be found beginning in the position it is in, but without capturing or advancing the reading of the string being analyzed, for example:

    Read words that happen before a point (.)
    Parsed string: 123. xpto.
    Regex: \b\w+\b(?=\.)
    Matches: 123, xpto

  • ?! Negative lookahead: This is an assertive, which checks whether the group can’t be found beginning in the position it is in, but without capturing or advancing the reading of the string being analyzed, for example:

    Read words that don’t happen before a point (.)
    Parsed string: 123 xpto abc.
    Regex: \b\w+\b(?!\.)
    Matches: 123, xpto

  • ?<= Positive lookbehind: This is an assertive, which checks whether the group can be found ending in the position it is in, but without capturing or advancing the reading of the string being analyzed, for example:

    Read words that happen after a point (.)
    Parsed string: abc. 123 xpto.
    Regex: (?<=\.\s*)\b\w+\b
    Matches: 123

  • ?<! Negative lookbehind: This is an assertive, which checks whether the group can’t be found ending in the position it is in, but without capturing or advancing the reading of the string being analyzed, for example:

    Read words that don’t happen after a point (.)
    Parsed string: abc. 123 xpto.
    Regex: (?<!\.\s*)\b\w+\b
    Matches: acb, xpto

If it’s of interest to you, I usually use this tool to work with regexes in C#:

http://rad-software-regular-expression-designer.software.informer.com/1.4/

  • Lots of ball show. + 1. Whenever I forget, I’ll come to this question

  • 2

    +1 I would just like to point out that this is not specific to . Net, but something standardized, available in a large number of Engines of regex. The support (mainly the lookbehind) varies, but the syntax is the same in all of them.

6

Lookahead is a way to find strings that have a certain ending or not. It is used (?⁼..) for the positive, that is to say, that they end with; and (?!..) to the negative, that is, that does not end with.

Lookbehind does the same as the Lookahead, however, as its name says, it does not search after, but before, the string said. (?<=..) to the positive and (?<!..) to the negative.

Example, consider the sequence foobarbarfoo.

bar(?=bar)    encontra o primeiro bar.
bar(?!bar)    encontra o segundo bar.
(?<=foo)bar   encontra o primeiro bar.
(?<!foo)bar   encontra o segundo bar.

You can also combine them:

(?<=foo)bar(?=bar)   encontra o primeiro bar.

See this tool online(Regexr) it helps you create expressions, such as identifying types, there are also examples.

Here explains in more detail about the subject.

Soon I will update the reply with more information.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.