0

I need to create a regex that do the matching of 2 chars: & OR |.

The line of code is like this:

boost::regex EXPR( "[0-9][0-9][A-Za-z]" ) ;

If I type any string, I would see if the chars listed above are contained in it. How is possible to do this?

3
  • 1
    Uuuuhm, sorry? I didn't get it. Commented Nov 20, 2011 at 18:02
  • If the string is "asd" no match. If the string is "aaa&sss" match. If the string is "asss|sss" match. If the string is "asss&sfs|ssf" match. Commented Nov 20, 2011 at 18:04
  • So, in words, "some letters, followed by at least one & or |, followed by more letters or & or | ? Commented Nov 21, 2011 at 8:08

3 Answers 3

4

I wouldn't write a regular expression to match the characters & or |.

I'd use std::string::find - http://www.cplusplus.com/reference/string/string/find/

Sign up to request clarification or add additional context in comments.

2 Comments

If you're looking for either of two characters, use find_first_of.
@MSalters: Yeah when I was looking at find I thought the char* overload would do that, but now that I'm looking at it again, find_first_of makes more sense.
1

Based on my interpretation of the grammar: [0-9A-Za-z]*[|&][0-9A-Za-z|&]*

Comments

0

This should work: ".*?\Q|\E.*". Note that you need to double the backslashes, so it is:

string yourCharacter("|"); // or "&"
string rex(".*?\\Q"+yourCharacter+"\\E.*");

(all characters before and after are matched by this regex)

Here is an online tool to test regular expressions: http://gskinner.com/RegExr/

1 Comment

Boost regex defaults to Perl syntax, which does have \Q...\E. But you don't need to escape characters inside a class, which is what you need here (One of two possible characters).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.