I'm dynamically making a regex.
I want it to match the following:
lem
le,,m
levm
lecm
Basically, "lem" but before the m it can have any number of ,
or any one of any character. Right now I have
le[\,]{0,}[.]?m
you can see it at http://regexr.com?303ne
It should match every one but the third one.
Update: I figured it out:
le[\,]{0,}.?m
Whenever you think "or" in Regular Expressions, you should start with alternation:
a|b
matches either a
or b
. So
any number of a list of characters OR 1 of any character
can be translated quite literally to
[...]*|.
where ...
would be the list of characters to match (a character class). If you use that as part of a longer expression, you need to use parentheses, because concatenation binds stronger (has higher precedence) than alternation:
le([,]*|.)m
Because the character class has only one item, we can simplify this:
le(,*|.)m
Note that .
by default means "any character but newline".
What about this:
le(,*|.?)m
it should do what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With