Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression: How to match using previous matches?

I am searching for string patterns of the form:

XXXAXXX 
# exactly 3 Xs, followed by a non-X, followed by 3Xs

All of the Xs must be the same character and the A must not be an X.

Note: I am not searching explicitly for Xs and As - I just need to find this pattern of characters in general.

Is it possible to build this using a regular expression? I will be implementing the search in Python if that matters.

Thanks in advance! -CS

Update:

@rohit-jain's answer in Python

x = re.search(r"(\w)\1{2}(?:(?!\1)\w)\1{3}", data_str)

@jerry's answer in Python

x = re.search(r"(.)\1{2}(?!\1).\1{3}", data_str)
like image 461
RobertJoseph Avatar asked Dec 06 '25 03:12

RobertJoseph


2 Answers

You can try this:

(\w)\1{2}(?!\1)\w\1{3}

Break Up:

(\w)        # Match a word character and capture in group 1
\1{2}       # Match group 1 twice, to make the same character thrice - `XXX`
(?!\1)      # Make sure the character in group 1 is not ahead. (X is not ahead)
\w          # Then match a word character. This is `A` 
\1{3}       # Match the group 1 thrice - XXX
like image 195
Rohit Jain Avatar answered Dec 08 '25 16:12

Rohit Jain


You can perhaps use this regex:

(.)\1{2}(?!\1).\1{3}

The first dot matches any character, then we call it back twice, make use of a negative lookahead to make sure there's not the captured character ahead and use another dot to accept any character once again, then 3 callbacks.

like image 24
Jerry Avatar answered Dec 08 '25 15:12

Jerry