0

I have a of regex patterns all of which I want to find in string and store the found pattern. I need all the possible matches of the regex in the given list, something like below.

regex = ["([a-zA-Z])*-([0-9])*","([0-9])*-([a-zA-Z]*)"]
s = 'This 24-hour journey is such a burden just get my I-20'
reg = re.compile('|'.join(regex))
arr = re.findall(reg,s)
print(arr)

But this gives me an output which is really weird. What I need as output is the list ['24-hour','I-20']. But what I am getting is this.

[('', '', '4', 'h'), ('I', '0', '', '')]

I think I am using the findall function badly but don't know how. Please help!

SierraD
  • 13
  • You need to remove capturing groups. Use `regex = ["[a-zA-Z]*-[0-9]*","[0-9]*-[a-zA-Z]*"]`, see [demo](https://ideone.com/Bw6RXF). – Wiktor Stribiżew Feb 20 '19 at 07:37
  • See the [docs](https://docs.python.org/3/library/re.html#re.findall), more specifically "If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group.", this excludes the actual match. – Jerry Feb 20 '19 at 07:41

0 Answers0