1

I need to convert string containing wildcards with a regexp.

For example, if I have an input string like below:

ab*dd*c

This means

ab(any number of any characters)d(any number of any characters)c

Where any character is any alphanumeric character or '_'.

I've tried something like below:

'ab([[:alnum]] | '_')*dd([[:alnum]] | '_')*c'

But, as you can see, for, example, ([[:alnum]] | '\_')* matches d also, and, so, I couldn't get needed match.

What comes to mind is use something like below:

'ab[^ d]*dd[^ c]*c'

Is there another better solution?

Thanks in advance!

Heghine
  • 435
  • 3
  • 15

1 Answers1

1

You can use this regex:

/ab\w*?dd\w*?c/

\w*? will match any alphanumeric character or _ non-greedy.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • yes, but my question is different. With your regex this `[\p{L}_]*` will match `dd` also, and we'll not get the needed match. – Heghine Jul 14 '15 at 05:59
  • Thanks, @anubhava, it's working. I meant that this `([[:alnum]] | '_')*` part of regex matches the whole word, and therefore, `dd` is not being matched. – Heghine Jul 14 '15 at 06:15
  • Even that can be written as: `[[:alnum:]_]*?` but `\w*?` is shorter. – anubhava Jul 14 '15 at 06:17
  • `[[:alnum:]_]*?` is not working, but `\w*?` is ok :) – Heghine Jul 14 '15 at 06:25