Question

i have four strings, which are "a.mob", "b.mob", "c.mob", "d.mob", "e.mob"

i want to match "a.mob", "b.mob", "d.mob" and any string end with ".mob" but except "c.mob" and "e.mob", i try to write a regex match:

"^(?!.*(c|e)).*?(?!mob)"

it done the work, but i think it hard to read and get.

in my mind, there maybe some simple match which is clear and simple.

my question is, is my match too complicate? is regex the best solution here?

Was it helpful?

Solution

welcome to SO.

If you want to "match a string ending with something", you have to use $ in your regex (and don't forget to quote the ., it's a special character in regexen):

"^[^ce]\\.mob$"

(otherwise you'll match biloximob5, for instance).

If your strings may be different than the five you have shown, maybe you'll have to be more generic:

"^([^ce]|..+)?\\.mob$"

for instance, matches .mob, elefante.mob, but does not match .mobi or e.mob...

May I suggest a regex-trying tool like regexr? It might be a good idea so you can train in the use of regexen.

Update: don't forget to quote the backslashes when you go back from perl/regexr to c++....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top