tags:

views:

53

answers:

1

Hi, I am new to regex expressions so sorry if this is a really noob question.

I have a regex expression... What I want to do is check if a string matches the regex expression in its entirety without the regex expression matching any subsets of the string.

For example...

If my regex expression is looking for a match of \s*A*\s*, it should return a match if the string it is comparing it to is " A " but if it compares to the string " A B" it should not return a match.

Any help would be appreciated? I code in C#.

+4  A: 

You would normally use the start end end anchors ^ and $ respecitvely:

^\s*A*\s*$

Keep in mind that, if you regex engine supports multi-line, this may also capture strings that span multiple lines as long as one of those lines matches the regex(since ^ then anchors after any newline or string-start and $ before any newline or string end). If you're only running the regex against a single line, that won't be a problem.

If you want to ensure that a multi-line input is only a single line consisting of your pattern, you can use \A and \Z if supported - these mean start and end of string regardless of newlines.

paxdiablo
Thank you.. that was it.
Mark Pearl