I'm not familiar with Regex so, I hope someone can help me with this. I'm using Regex and I'm checking further in the code if that specific Regex is a match or not.
Regex I use and want to check for:
Regex InitialGForKj = new Regex("[Y[\\s\\S]|E[BILPRSY]|I[BELN]]", RegexOptions.Compiled);
. Now the code where I check if it is a match is as followed:
else if (i == 0 && InitialGForKj.IsMatch(upperLetters.Substring(1, 3)))
{
dmetaphoneKeyResultPrimary.Append("K");
dmetaphoneKeyResultSecondary.Append("J");
i += 2;
}
i
is the current Character (In this case it is 'G'). upperLetters
= The Word what will be checked (In this case: Garçon). dmetaphoneKeyResultPrimary
and dmetaphoneKeyResultSecondary
are the replacements for the Letter 'G' (Both are Stringbuilders). I Also checked with Console.WriteLine()
what the substring its value is (Which returns "arç"). So, now the question is, why does the code see this as true
and not as False
? Which I think it must be False
.
Or am I doing it completely wrong?
You must write it as
var InitialGForKj = new Regex("Y.|E[BILPRSY]|I[BELN]", RegexOptions.Compiled | RegexOptions.Singleline);
See the regex demo . Note that .
matches any char including a newline since the RegexOptions.Singleline
option is passed to the Regex constructor.
The current [Y[\\s\\S]|E[BILPRSY]|I[BELN]]
pattern matches 3 alternatives:
[Y[\\s\\S]
- a Y
, [
, whitespace or non-whitespace (this matches any char there is!) |
- or E[BILPRSY]
- E
and then one letter from the class |
- or I[BELN]]
- a I
followed with B
, E
, L
, N
, and then ]
. As you see, you corrupted it by enclosing the whole pattern with [
and ]
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.