-3

I tried this


pattern_time = (
    r"\b\d{1,2}([: .]?)(\d{2})?(\s?)((P|p)?)(.?)((M|m)?)(.?)((next|this)?)(\s?)((tomorrow|today|day|evening|morning|(mid?)night|((after|before)?)(noon))?)\b"
)

test_string = "The meeting is at 3pm today or 5 tomorrow or 7 this afternoon or 00:00 midnight. Let's meet at 11.30 p.M. We can also do 8:45 pm or 1200 hr or 00hr."

matches = re.findall(pattern_time, test_string, re.IGNORECASE)
print(matches)

and obtained in python

[('', '', '', 'p', 'p', 'm', '', '', ' ', '', '', '', 'today', 'today', '', '', '', ''), (' ', '', '', '', '', '', '', '', '', '', '', '', 'tomorrow', 'tomorrow', '', '', '', ''), (' ', '', '', '', '', '', '', '', '', 'this', 'this', ' ', 'afternoon', 'afternoon', '', 'after', 'after', 'noon'), (':', '00', ' ', '', '', '', '', '', '', '', '', '', 'midnight', 'midnight', 'mid', '', '', ''), ('.', '30', ' ', 'p', 'p', '.', 'M', 'M', '.', '', '', ' ', '', '', '', '', '', ''), (':', '45', ' ', 'p', 'p', 'm', '', '', ' ', '', '', '', '', '', '', '', '', ''), ('', '00', ' ', '', '', 'h', '', '', 'r', '', '', ' ', '', '', '', '', '', ''), ('', '', '', '', '', 'h', '', '', 'r', '', '', '', '', '', '', '', '', '')]

but regextest is showing that it is correct.

How can we rectify this in Python?

Thank you for your help.

enter image description here

Ursa Major
  • 851
  • 7
  • 25
  • 47
  • Whats `regextest`? – Nitish Apr 22 '23 at 05:59
  • it is an application that checks the regex, such as regextester.com. Some one solved it. It is the syntax of the next line. The regex is ok, it is the next line that needs to be rectified. – Ursa Major Apr 22 '23 at 07:20

1 Answers1

1

re.findall will give the values for each capture group. To only get the entire match, you can use re.finditer with a list comprehension.

matches = [m.group() for m in re.finditer(pattern_time, test_string, re.I)]

Alternatively, you could restructure your regular expression to use non-capturing groups. (That would be tedious here as it would require replacing each () group with (?:).)

See the documentation for re.findall:

The result depends on the number of capturing groups in the pattern. If there are no groups, return a list of strings matching the whole pattern. If there is exactly one group, return a list of strings matching that group. If multiple groups are present, return a list of tuples of strings matching the groups.

Unmitigated
  • 76,500
  • 11
  • 62
  • 80