Jessica
Jessica

Reputation: 31

Search pattern in text

I have developed this code to find a pattern in a text:

pattern = re.compile(r'\: (\d{2})/(\d{2})/(\d{4})')
match = re.search(pattern, txt)

My pattern is a date like this: dd/mm/yyyy . The problem is the following: In the text might appear two dates but I want to get just one. The difference between both is the text before the date. I mean:

text1: dd/mm/yyyy
text2: dd/mm/yyyy

I just want to get the date with the text2 before. How can I do that?

Upvotes: 2

Views: 88

Answers (2)

Aleksandar Makragić
Aleksandar Makragić

Reputation: 1997

You could put every date you find in list, and then take the last one.

list_of_dates = []
pattern = re.compile(r'\: (\d{2})/(\d{2})/(\d{4}))') 

for date in pattern.finditer(txt):
   list_of_dates.append(date.group(1)) # Take date as back reference

list_of_dates[-1] # This would give you last date

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627468

Use text2 in the pattern and capture the date subpattern:

import re
txt = """text1: 12/05/2015
text2: 22/05/2016"""
pattern = re.compile(r'text2:\s*(\d{2}/\d{2}/\d{4})')
match = re.search(pattern, txt)
if match:
    print(match.group(1))

See the Python demo

Details:

  • text2: - a literal substring
  • \s* - 0+ whitespaces
  • (\d{2}/\d{2}/\d{4}) - capturing group 1 that matches 2 digits, /, 2 digits, / and then 4 digits.

The re.search method will find the first match, and if found, we need to get the contents of the first capturing group (match.group(1)).

Upvotes: 3

Related Questions