I need to build a regex that match words with these patterns:
Letters and numbers:
A35, 35A, B503X, 1ABC5
Letters and numbers separated by “-“, “/”, “\”:
AB-10, 10-AB, A10-BA, BA-A10, etc…
I wrote this regex for it:
\b[A-Za-z]+(?=[(?<!\-|\\|\/)\d]+)[(?<!\-|\\|\/)\w]+\b|\b[0-9]+(?=[(?<!\-|\\|\/)A-Za-z]+)[(?<!\-|\\|\/)\w]+\b
It works partially, but it’s match only letters or only numbers separated by symbols.
Example:
10-10, open-office, etc.
And I don’t wanna this matches.
I guess that my regex is very repetitive and somewhat ugly.
But it’s what I have for now.
Could anyone help me?
I’m using java/groovy.
Thanks in advance.
Interesting challenge. Here is a java program with a regex that picks out the types of “words” you are after:
Here is the correct output:
A35, 35A, B503X, 1ABC5, AB-10, 10-AB, A10-BA, BA-A10,Note that the only complex regexes which are “ugly”, are those that are not properly formatted and commented!