0

I've been trying to match a pattern excluding a certain char.

Lets say the characters given is "h...o", and I want to match all words not including a certain char, let's say 'c' I've tried a ton of different ways of going about this. Some of those include,

"h^[c]^[c]^[c]o"

"h(!?c)(?!c)(!?c)o"

But none of those works, does anyone know whats up?

the point of this is kind of like a cheating hang man, where I return all the words not containing the guess.

vector<string> getWords(string currentWord, char guess){
    for(auto& i: currentWord){
        string reg = ""
        if(i == '_'){
           string t = [[regex expression for acception any char except 'guess']]
           reg += t;
        }else{
          reg += i;  
        }
    }

  return getMatchingWords(mapOfWords, reg)
}

Only accepting all words that doesn't contain the guess doesn't work here as the exclusion is indexbound

AlexVestin
  • 2,548
  • 2
  • 16
  • 20
  • Brute force does not really help here :) What do you consider a word? How can you define the boundaries for these "words"? I am asking because `.` is not a word char (from the regex point of view) – Wiktor Stribiżew Sep 13 '17 at 12:14
  • it's from a dictionary – AlexVestin Sep 13 '17 at 12:15
  • Could you please provide a code snippet illustrating the problem? An [MCVE (minimal complete verifiable example)](http://stackoverflow.com/help/mcve). – Wiktor Stribiżew Sep 13 '17 at 12:16
  • edited the uestion – AlexVestin Sep 13 '17 at 12:20
  • Sorry, does not it mean you iterate over the string and check some specific substrings for the presence of the `guess` char? You might check [Check if a string contains a string in C++](https://stackoverflow.com/questions/2340281/) rather than look for a regex. – Wiktor Stribiżew Sep 13 '17 at 12:28

1 Answers1

2

The regular expression h^[c]^[c]^[c]o doesn't work, because it looks for h, followed by a ^, followed by a c, and so on.

To negate a character set, you must put the caret ^ inside the brackets, see Negated Character Classes. In your case, this would be

h[^c][^c][^c]o

Now it looks for h, followed by any character not c, followed by another character not c, ...

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198