Passing a command line argument into a regex expression

I am trying to get a user to enter a basic regex expression to search for files of a specific extension. So they would enters something like this:
 
  ".(docx|doc)" 


Into the command line as an argument after a certain regex switch has been flagged. For some reason when I run my program and enter a regex expression like above, it will not go through with the rest of the code. Here is the most important part of the code so far:
1
2
3
4
5
6
7
8
9
  else if( x_flag_set == true )
  {
    if( j_flag_set == true || c_flag_set == true || m_flag_set == true )
    {
      cerr << "ERROR: cannot combine x, m, c, or j switches." << endl;
      return EXIT_FAILURE;
    }    
    regSearch = ( argv[2] );
  }


Just a note: regSearch is globally declared and if i pass in a hard coded value like the docx/doc under the x_flag_set it will work. It's just passing in a command line argument.
This requires the regex to be exactly the second argument. Is that always true?
You're not giving much information. What does it mean "it will not go through"?

This is a good chance to start using a debugger, if you never did.
Ah nevermind I just realized I made a silly mistake. I have it so that code is based off the argc count and this code is contained in an argc == 2 if, which rules out having a third argument for the regex, which is why this is not working.

EDIT: Nevermind, even if I alter the code, it still will not work
Last edited on
Entering a regular expression as a command-line argument is almost always a mistake, because users then have to contend with proper quoting. Even the standard grep program is not immune to the difficulties.

I'm not sure what to tell you is wrong. Have you tried printing the regex string to see that it contains what you think it does?
Topic archived. No new replies allowed.