You need to use the or function in regexes.
@strings = ('A A C D','A A/B C D','A/B A C D','A/B A/B C D'); foreach $string (@strings) { ($third) = $string =~ m/(?:A|A\/B) (?:A|A/B) (\w+) \w+/; printf "%s - %s\n", $string, $third }
A A C D - C A A/B C D - C A/B A C D - C A/B A/B C D - C
I put the two alternatives in a group separated by a |, which is the or function. I put them in a group so that it is clear these two alternatives apply only for the first match in the line. The ?: at the beginning means that group should be used for matching purposes but the value should not be captured. I repeat that group because the same two alternatives may show up as the second match.
In reply to Re: Regex not matching as expected
by GotToBTru
in thread Regex not matching as expected
by Fisherman166
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |