tags:

views:

66

answers:

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

+1  A: 

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

suzanshakya
+1  A: 
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")

ΤΖΩΤΖΙΟΥ