2

I'm trying to use re.findall() find all the occurrences of the names of weekdays. It works when I exclude the \b, but not when I include them. This works:

any_week_day_long = "([Mm]onday|[Tt]uesday|[Ww]ednesday|[Tt]hursday|[Ff]riday|[Ss]aturday|[Ss]unday)"
match = re.findall(any_week_day_long, "Monday is a great day of the week. Tuesday is pretty good, but Wednesday has it beat.")

but this does not:

any_week_day_long = "\b([Mm]onday|[Tt]uesday|[Ww]ednesday|[Tt]hursday|[Ff]riday|[Ss]aturday|[Ss]unday)\b"
match = re.findall(any_week_day_long, "Monday is a great day of the week. Tuesday is pretty good, but Wednesday has it beat.")

It seems to me like it should find Monday, Tuesday, and Wednesday just fine with the \b, but when I print match, it's just an empty list.

user3210986
  • 211
  • 2
  • 9

2 Answers2

2

Instead of using \b try: \\b

any_week_day_long = "\\b([Mm]onday|[Tt]uesday|[Ww]ednesday|[Tt]hursday|[Ff]riday|[Ss]aturday|[Ss]unday)\\b"
match = re.findall(any_week_day_long, "Monday is a great day of the week. Tuesday is pretty good, but Wednesday has it beat.")

OUTPUT

['Monday', 'Tuesday', 'Wednesday']
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0

You can even achieve the same using raw string. Instead of doing something like [M|m], it's much better use the re.IGNORECASE flag for the same. A much cleaner way to do the same.

any_week_day_long = r'\b(?:mon|tues|wednes|thurs|fri|satur|sun)day\b'
match = re.findall(any_week_day_long, "Monday is a great day of the week. Tuesday is pretty good, but Wednesday has it beat.")  

Output:

['Monday', 'Tuesday', 'Wednesday']
taurus05
  • 2,491
  • 15
  • 28