2

I want to match certain symbols only when they are not prefixed by specific characters. For instance, match "))))))))))" when it is not preceded by "x". Need some advices. My current expession is

(?<!x|X|:|=|\\1)([\|()\[\]])+

which does not work.

[EDIT] Rephrase my question

5
  • 1
    What exactly do you wish to accomplish? Commented Oct 19, 2010 at 9:59
  • @BoltClock, i want to match the items in the captured group like '))))))))' in 's)))))))' when they are not prefixed by the characters in the lookbehind group/ Commented Oct 19, 2010 at 10:05
  • 3
    Python doesn't support forward references. Commented Oct 19, 2010 at 10:14
  • 1
    For the ')))' case, the following may do what you need, (?x) (?<! [xX:=)]) ( [][()|]+ ) Commented Oct 19, 2010 at 10:23
  • @ar,thanks for your answer... Commented Oct 19, 2010 at 10:50

2 Answers 2

1
re.search(r"(?<![x)])\)+", text)

>>> re.search(r"(?<![x)])\)+", " hello)))))")
<_sre.SRE_Match object at 0xb75c0c98>
>>> _.group()
')))))'
>>> re.search(r"(?<![x)])\)+", " hellox)))))")
>>>

This makes use of the “negative lookbehind assertion”: we want as many parentheses as possible, not preceded by either "x" or ")" (the latter because otherwise, we would get the parentheses starting from the second parenthesis, preceded by the first parenthesis and therefore not an "x")

Sign up to request clarification or add additional context in comments.

Comments

1

Use complementing character class: '[^x\)](\)+)'
All your specific characters which should not be prefixed will be placed with x, along with ).

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.