0

I want to write a regular expression that will match the following string

a (any substring except 'ABC') ABC

An example for this would be a pqrs h js ABC The tricky part is to match any substring except 'ABC'. Since the document in which I am searching for, can contain multiple lines that contain such pattern and I want to find all the lines separately I can't use the following expression

a.*ABC

because this would just give me the line where the first a is found extending uptill where the last 'ABC' is found in the document.

There is this answer which says I can use look ahead negation but that is not working in python, or maybe in my case because there is substring before and I have not tested simply using that expression because it will not serve my purpose

Community
  • 1
  • 1
Sachin
  • 3,672
  • 9
  • 55
  • 96

1 Answers1

2

Use the non greedy quantifier i.e ?

^a.*?ABC
Toto
  • 89,455
  • 62
  • 89
  • 125
  • Thanks a lot, It worked. I am new at regular expressions, if possible can you explain why `?` is called non greedy quantifier – Sachin Mar 07 '12 at 16:22
  • 1
    @Sachin: Have a look at this site http://www.regular-expressions.info/tutorial.html , it will explain better than I. – Toto Mar 07 '12 at 16:31