regex – python regular expression 1

regex – python regular expression 1

1 is equivalent to, the first parentheses-delimited expression inside of the regex.

Its also, fun fact, part of the reason that regular expressions are significantly slower in Python and other programming languages than required to be by CS theory.

The first 1 means the first group – i.e. the first bracketed expression (b[a-z]+)

From the docs number

Matches the contents of the group of the same number. Groups are numbered starting from 1. For example, (.+) 1 matches the the or 55 55, but not thethe (note the space after the group)

In your case it is looking for a repeated word (well, block of lower case letters).

The second 1 is the replacement to use in case of a match, so a repeated word will be replaced by a single word.

regex – python regular expression 1

1 is a backreference.
It matches, what ever matched in your brackets, in this case the

You are basically saying

  • match empty string at the beginning of a word (b)
  • match alphabetical characters from a-z, one or more times
  • match the term in brackets again

cat in ( the) the hat

Leave a Reply

Your email address will not be published. Required fields are marked *