I am now using regex to handle some string, now I have a special string that is not easy to use regex to handle. below are some sample string. TP-LINK is a sample, it can be other router vendor with similar format.
TP-LINK Wireless Range Extender WA810RE
TP-LINK Wireless WA850RE
TP-LINK Wireless Range Extender WA850RE
TP-LINK Wireless Range Extender WA890RE
As you can see in above string, I want to get
TP-LINK which will use
TL instead, and
WA850RE, then the final result is
I am using look ahead in regex, but it seems not work.
re1 = 'TP-LINK Wireless Range Extender WA850RE'
m1 = re.search('(T(?=P-)L(?=INK)(.+)(WA\\w+))', re1)
Best How To :
Some notes about your original regex:
- a lookahead only makes sense at the end of the string; you were probably looking for a non-capturing group, e.g.
T(?:P-) instead of
T(?=P-), but you don't even need those if they appear exactly once (i.e. if there's no need to put a
? behind the group)
" are not actually a part of the string, so you should remove those from the regex
- only put capturing groups around those parts that you want to use in the result.
Put together, you could do this:
>>> m = re.search(r'(T)P-(L)INK.+(WA\w+)', re1)
However, if you want to abbreviate the string, you should just match the string and extract the important parts using a regex, and then prefix the result. There's no need to capture the
TP-LINK part in a group if it's always the same anyway. This will also further simplify your regex.
>>> m = re.search(r'TP-LINK.+(WA\w+)', re1)
>>> "TL" + m.group(1)
And then similar regexes for the other cases (Netgear, Cisco, etc.)