3

I have the following code

// the values for the input and the pattern 
// are combinations of R, NR and HR
var input = "NR|HR"; 
var pattern = "R";

var isMatch = Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase); 

this returns true because NR and HR contains an R

Is there some way I can do an exact match for R with Regular Expressions?

1
  • 2
    Do you actually require regular expression? Why don't you simply do var containsR = input.Split('|').Contains('R')? Commented May 17, 2013 at 14:23

3 Answers 3

4

You can use word boundaries:

var pattern = "\bR\b";
Sign up to request clarification or add additional context in comments.

Comments

0

Start of string ^ and End of string $

var pattern = "^R$";

1 Comment

doesn't handle R|HR though
0

Try the following:

'(^R\|)|(\|R$)|(\|R\|)|(^R$)'

It picks our R from the following

R|NR
NR|R
R

but not the following

HR|NR
HR

Comments

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.