0

Possible Duplicate:
Create regex from glob expression

We know that Bash doesn't use regular expressions for matching file names. For example: *.dat would match all file names with the dat extension. However, the same pattern in regular expression form would have been: .*\.dat

My question is how can I take a Bash-style pattern and translate it into a valid regular expression that would match the same file names.

For simplicity sake, let's ignore all sorts of Bash expansions and let's assume that Bash does not treat . .. and / separately.

Community
  • 1
  • 1
Shahriar
  • 280
  • 2
  • 9

1 Answers1

0

I think that should not be too complicated. The non-special characters can simply be left as they are.

The special symbols may have to be translated to "usual" regular expressions.

Exemplary, if we consider *.dat, we have to convert the * into .* and the . to \.. Moreover we have to change ? (match one character) to .. Character groups [abc] should essentially stay the same, probably adjusting some special symbols. And you'll have to recognize escaped characters, e.g. \* and treat them adequate (in case of \*, just leave it there).

So, all you have to do is create a list of special symbols and their corresponding equivalents in usual regular expressions.

However, you should definitely have a look at the link provided by @WoLpH.

Community
  • 1
  • 1
phimuemue
  • 34,669
  • 9
  • 84
  • 115