SoFunction
Updated on 2025-04-17

Usage of regular expressions?: ?=?! in python

Used in regular expressions?:?=and?!is three different regular expression syntaxes that represent non-capturing groups, positive lookahead assertions, and negative lookahead assertions.

1. ?: Usage

(?:...)is a non-capturing group syntax. It is used to enclose a portion of the regular expression together, but does not use that part as a separate capture group. This means that even if this part of the expression is matched, no new capture group is created in the result.

The capture group in a regular expression is a part of the expression surrounded by parentheses (). These capture groups mark specific parts in the matching string and save them for later use.

If you are not sure what a capture group is, you will be clear with an example below:

import re

pattern = r'a(.*)test'
text = 'This is a good test.'

match = (pattern, text)
if match:
    print("Found word:", ())
#Output# Found word: (' good ',)

In this example,(.*)is a capture group used to convertaandtestExtract the characters in the middle.

Once you know the capture group, you can guess it even if you don’t capture group. Sometimes you might just want to enclose the expressions, but don't want to save the matching results. In this case, non-capturing groups can be used(?: ...)

Example:

import re

pattern = r'(?:abc)def'
text = 'abcdef'

match = (pattern, text)
if match:
    print("Match found")

In this example,(?:abc)is a non-capture group, which willabcanddefCombined together for matching, but not as a separate capture group.

2. ?= Usage

?=It is a positive lookahead assertion. It is used to match the position after a subexpression, but does not contain the matching subexpression in the final result. Preview assertions are used to ensure that a certain part of the match is matched by the subsequent part of the condition.

Example:

import re

pattern = r'abc(?=def)'
text = '.x abcdef123'
text2 = '.x abc1def123'

match = (pattern, text)
if match:
    print("Match found:", ())
#Output# Match found: abc

match = (pattern, text2)
if not match:
    print("Match not found")
#Output# Match not found

In this example,abc(?=def)The positive lookahead assertion was used. It will matchabcFolloweddefpart. existabcdefIn, it matchesabc,becauseabcNext isdef

3. ?! Usage

?!is a negative lookahead assertion. It is used to ensure that a subexpression does not match the position after it does not contain the matching subexpression in the final result.

Usage example:

import re

pattern = r'abc(?!def)'
text = 'abcxyz'
text2 = 'abcdef'

match = (pattern, text)
if match:
    print("Match found:", ())
#Output# Match found: abc

match = (pattern, text2)
if not match:
    print("Match not found")
#Output# Match not found

In this example,abc(?!def)Negative lookahead assertions were used. It will matchabcNot followingdefpart. existabcxyzIn, it matchesabc,becauseabcNo laterdef

4. Summary

  • (?:...)It is a non-capturing group and the matching results within the group will not be captured.
  • ?=It is a positive lookahead assertion to ensure that the matching position meets the conditions.
  • ?!It is a negative lookahead assertion to ensure that the matching position does not meet the conditions.

This is the article about the usage of regular expressions in python?: ?= ?! This is all about this article. For more related regular expressions in python?: ?= ?! For content, please search for my previous article or continue browsing the following photos.