Yoda
Yoda

Reputation: 339

Regular Expression issue in C# returning match true

This is the regular expression i have written and it returns true if "$" or "^" is passed.Why does it return true when i specified only the below characters should match

public static bool IsTrue(string test)
{
    Regex regex =  new Regex(@"(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)?/{0,2}[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?");    
    return regex.IsMatch(test);
}

Upvotes: 1

Views: 413

Answers (2)

p.s.w.g
p.s.w.g

Reputation: 149020

The problem is that all parts of your regex are optional, that means that even an empty string will match your regex. Even if some invalid string were passed in, it would still match a zero-length substring at the start of the input.

Also, you've escaped the slashes. This is not necessary in a verbatim string literal (a string literal starting with @). The string regular string literal "foo\\bar" is equivalent to to the verbatim literal @"foo\bar" .

I think you'll want to add start (^) and end ($) anchors to your pattern:

new Regex(@"^(([a-zA-Z][0-9a-zA-Z+\-.]*:)?/{0,2}[0-9a-zA-Z;/?:@&=+$.\-_!~*'()%]+)?(#[0-9a-zA-Z;/?:@&=+$.\-_!~*'()%]+)?$");

This will make ^ fail, but an empty string still matches (since all parts are still optional) and $ is still valid input. It's hard to tell exactly how to modify this pattern without more information on what you want to match and what you don't want to match.

According to your comments, if you simply want to restrict input to a few characters, you can simply use the pattern:

new Regex(@"^[A-Za-z0-9 '()&#/\\+;:-]*$");

Upvotes: 3

Matthew
Matthew

Reputation: 9949

If you look at your match, every group ends with a ? making all parts optional:

(
    (
        [a-zA-Z][0-9a-zA-Z+\\-\\.]*:
    )?
    /{0,2}[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+
)?
(
    #[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+
)?

I had to reformat as above to confirm it.

Upvotes: 1

Related Questions