RegEx Help Question

Hey guys,

For you RegEx experts out there.

I need an expression where I am searching for the following possible answers in response to a command:

y
n
\x

If I do something like:

[yn\\x]

It doesn’t work to pick up \x. It just gets \.

How do I get it to treat “\x” as a pair of characters to look for. I’ve tried grouping and a few other things, but can’t get it to work.

Thanks,

Jon

I remember seeing here on the forum that regex is NOT a parser and should not be use that way…
use a real parser for that.

(y|n|\\x) works for me

[quote=262333:@Jean-Yves Pochez]I remember seeing here on the forum that regex is NOT a parser and should not be use that way…
use a real parser for that.[/quote]

That’s not what I am doing.

A character class (square brackets surrounding one or more tokens) will only match one character in the source text. Every token within it is treated individually so your pattern will match either a backslash or an x.

Mark’s suggestion is a good one, and you can also do this:

[yn]|\\\\x

If that constitutes the entire patten, no parens are needed.

Thanks guys, as always. Yes, there are other parts to the pattern. This is just the part where I was stuck.

I hadn’t seen the “|” used before. I typically use Kem’s RegExRX pattern help but didn’t see that one in there…

[quote=262338:@Kem Tekinay]A character class (square brackets surrounding one or more tokens) will only match one character in the source text. Every token within it is treated individually so your pattern will match either a backslash or an x.

Mark’s suggestion is a good one, and you can also do this:

[yn]|\\\\x

If that constitutes the entire patten, no parens are needed.[/quote]
I was not aware that the ‘|’ could be used without the parens, I’ve only seen it written as ‘(a|b)’. Thanks.

You’re right Jon, the alternator is not there. I’ll have to add that to a future version.