1

I would like to retrieve everything before a specific string in a text file using regex in Python. For example with the line

text = 'What I want is a red car'

I would like to retrieve everything that is before "a red car", that is:

result = 'What I want is'

The whole string "a red car" is important, not only "red" or "car" separately!

Thanks in advance!

Batmax
  • 253
  • 8
  • 17

3 Answers3

4

If you need to use a regex for this :

regex = re.compile('(?P<before_red_car>.+) a red car')
regex.search("What i want is a red car").group('before_red_car')

If you don't want to name your group :

regex = re.compile('(.+)a red car')
regex.search("What i want is a red car").group(1) 

If you need to catch everything including newlines, add the re.DOTALL flag.

However, doing

text = 'What I want is a red car'
text.split('a red car')[0]

Or even :

text = 'What I want is a red car'
text.replace('a red car', '')

Work too, and are arguably easier to understand. They are also twice faster :

timeit.timeit(lambda: text.split('a red car')[0])
0.5350678942020507

timeit.timeit(lambda: text.replace('a red car', ''))
0.5115460171814448

timeit.timeit(lambda: regex.search("What i want is a red car").group(1))
1.123993800741033 

# Without re.compile()
timeit.timeit(lambda: re.search('(.+)a red car', text).group(1))
1.94518623436079
Unatiel
  • 1,060
  • 1
  • 11
  • 17
1

You can try this:

strIn = 'What I want is a red car'
searchStr = 'a red car'
print(strIn[:strIn.find(searchStr)])
nanojohn
  • 572
  • 1
  • 3
  • 13
-2

This might help

text = 'What I want is a red car'
print(text[0:13])
Tanmay Mane
  • 67
  • 10
  • And what happens if the text is then changed to `What you want is a red car`? – Peter Featherstone Aug 03 '17 at 17:16
  • you mean that text = 'What you want is a red card?' – Tanmay Mane Aug 03 '17 at 17:17
  • Exactly that, you will end up returning `What you want`... My point being, that this solution is not dynamic or flexible. Also, I just tested the above and it returns `What I want i`, which doesn't seem like what the OP wants – Peter Featherstone Aug 03 '17 at 17:19
  • yeah your right. But in the question he has not mentioned about any situation like that. Plus he himself is declaring the string text. That's why i came up with this soln. – Tanmay Mane Aug 03 '17 at 17:23