Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression listing all possibilities

Tags:

regex

Given a regular expression, how can I list all possible matches? For example: AB[CD]1234, I want it to return a list like: ABC1234 ABD1234

I searched the web, but couldn't find anything.

like image 304
gmuller Avatar asked Nov 03 '09 14:11

gmuller


People also ask

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

What does $1 do in regex?

For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.

Which are 3 uses of regular expression?

Regular expressions are useful in any scenario that benefits from full or partial pattern matches on strings. These are some common use cases: verify the structure of strings. extract substrings form structured strings.

Is a * b * a regular expression?

Regular expressions are equal if and only if they correspond to the same language. Thus for example ( a + b )* = ( a*b* )* , because they both represent the language of all strings over the alphabet {a, b}.


2 Answers

Exrex can do this:

$ python exrex.py 'AB[CD]1234'
ABC1234
ABD1234
like image 132
Sjoerd Avatar answered Oct 21 '22 19:10

Sjoerd


The reason you haven't found anything is probably because this is a problem of serious complexity given the amount of combinations certain expressions would allow. Some regular expressions could even allow infite matches:

Consider following expressions:

AB[A-Z0-9]{1,10}1234

AB.*1234

I think your best bet would be to create an algorithm yourself based on a small subset of allowed patterns. In your specific case, I would suggest to use a more naive approach than a regular expression.

like image 42
Yannick Motton Avatar answered Oct 21 '22 21:10

Yannick Motton