I have written 2 REs to match several string sequences in a String. for e.g. lets assume the two regular expressions are RE1
, RE2
. The strings can be in these 4 forms;
1) Match ONLY RE1 'one or more times' 2) Match ONLY RE2 'one or more times' 3) Match RE1 'one or more times' AND match RE2 'one or more times' 4) Match NEITHER RE1 NOR RE2
currently I am using if
to check each of these, but I know its very expensive as I am doing the matching for a particular string several times. I thought of using 'or' |
but the problem with that is regex will stop matching once it finds the first matching sequence and not continue to find others. I want to find matching sequences 'one or more times'.
Update:
eg: RE1 = (\d{1,3}[a-zA-Z]?/\d{1,3}[a-zA-Z]?) RE2 = (\babc\b) String: *some string* 100/64h *some string* 120h/90 *some string* abc 200/100 abc *some string* 100h/100f Matches: '100/64h', '120h/90', 'abc', '200/100', 'abc', '100h/100f'
How can I merge these 2 REs to make my program efficient. I am using python to code this.