3

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)
6
  • In addition to consulting the answers below, you may wish to look at docs.python.org/2/library/re.html#regular-expression-syntax Aug 28, 2013 at 16:50
  • You can use x = re.search(r"(.)\1{2}(?!\1).\1{3}", data_str) to avoid double escaping.
    – Jerry
    Aug 28, 2013 at 17:06
  • Sweet - thanks Jerry! Aug 28, 2013 at 17:15
  • @CaymanEss Also, I'm a bit confused as to why you put \n in the negative lookahead, because it actually breaks the regex, see this. If you remove the \n, it works fine, see this.
    – Jerry
    Aug 28, 2013 at 17:21
  • @Jerry Typo... sorry. :( Aug 28, 2013 at 18:00

2 Answers 2

9

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
1
  • 3
    You might want to rephrase "word" as "word character", just to be clear. Aug 28, 2013 at 16:56
4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.