-2

I'm trying to select with regular expression any file with db extension. My regex is:

*.db  

yet I'm getting error saying that '*' cannot start regular expression? How so, every time I type something like *.exe let's say in windows explorer search box I'm getting what I want, that is every file with .exe extention.
I'm using boost::regex.

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
  • 2
    `*.db` is not a regex pattern, it is a wildcard pattern. To match 0+ characters, use `.*`. In your case, I guess you need `^.*\.db$` (or `^.*[.]db$` if you do not know how to properly escape backslashes in C strings). – Wiktor Stribiżew Jun 02 '16 at 09:07
  • @WiktorStribiżew Hi, Thanks for your answer. But I cannot expect user to type regular expression in a search box. User *will* expect the wildcard pattern to just work. Is there a way to translate wildcard pattern into regex? – There is nothing we can do Jun 02 '16 at 09:10
  • Replace `*` with `.*`, `?` with `.`, handle other wildcard subpatterns, and escape special characters. – Wiktor Stribiżew Jun 02 '16 at 09:11
  • @WiktorStribiżew thanks, will do. On the other hand it is somewhat illogical that * cannot mean in regex what it mean in wildcard pattern. – There is nothing we can do Jun 02 '16 at 09:12
  • These are completely different (but similar in their weird appearance) kinds of patterns. So, that is logical. – Wiktor Stribiżew Jun 02 '16 at 09:13
  • @WiktorStribiżew any logical reason why * coulnd't mean: any char any number of times? – There is nothing we can do Jun 02 '16 at 09:14
  • 1
    @Thereisnothingwecando _"any logical reason why * coulnd't mean: any char any number of times? "_ Because regular expression syntax uses `.*` for this. – πάντα ῥεῖ Jun 02 '16 at 09:18
  • 1
    @Thereisnothingwecando: There is the same logic as in the fact that "By" does not denote the same thing in English and in Polish. – Wiktor Stribiżew Jun 02 '16 at 09:20
  • 1
    @Thereisnothingwecando See [here](https://en.wikipedia.org/wiki/Glob_(programming)#Compared_to_regular_expressions) – Lucas Trzesniewski Jun 02 '16 at 09:21
  • @WiktorStribiżew so basically no logical reason, simply different (longer) way of expressing the same? – There is nothing we can do Jun 02 '16 at 09:27
  • @LucasTrzesniewski Thanks/Dzięki – There is nothing we can do Jun 02 '16 at 09:27
  • @WiktorStribiżew Just to make sure, I'm not trying to have an argument and I'm very grateful for your help (thank you), only I want to understand. Thanks again for your help. – There is nothing we can do Jun 02 '16 at 09:28
  • 1
    @Thereisnothingwecando consider that "any char any number of times" is actually 2 things: "any char" and "any number of times". Expressing this with a single character, like the `*`, is limiting your choices. What if you wanted to express "any char, between 3 and 5 times"? With the Windows explorer syntax you can't do that. With a regex you can write `.{3,5}`. Or: "just a vowel, any number of times": `[aeiou]*`. Again, with the other syntax you can't. So the answer is: regular expressions are more complicated because they are more powerful. Windows gives you something easier but more limited. – Fabio says Reinstate Monica Jun 02 '16 at 09:33

1 Answers1

1

I think you would need:

"(.*\\.db)"

Reference: Click here

Community
  • 1
  • 1
Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105