Ross Cooper
Ross Cooper

Reputation: 245

Regex IsMatch Issue

Can someone tell me the error in Regex for below mentioned things:

  1. Regex: @"^(tcm:\d+-\d+)"

    Input String: tcm:12-123a6

    Problem: \d should only match numerals as per my knowledge. Input string has 'a' in it. Still it matches the input string.

  2. Regex: @"^[a-zA-Z0-9,&\s-]*$"

    Input String: Transportation, Tourism & Travel which I am reading from Query String and comes as Transportation%252c%2bTravel%2b%2526%2bTourism

    Problem: I think I have taken all the characters of input into Regex. Still it does not match.

  3. Regex: @"^[a-zA-Z0-9=]*$"

    Input String: U2VuaW9yIFBhcnRuZXIgJiBNYW5hZ2luZyB&&&EaXJlY3Rvcg==

    Problem: Even with '&' in input, why is it matching?

Upvotes: 2

Views: 214

Answers (1)

Ulugbek Umirov
Ulugbek Umirov

Reputation: 12807

  1. @"^(tcm:\d+-\d+)" will match tcm:12-123 from your string, you need to put $ in the end of your regex to match whole string.

    @"^(tcm:\d+-\d+)$"

  2. If ':' belongs to the string, then you need to add it to your list.

    @"^[a-zA-Z0-9,&\s-:]*$"

Upvotes: 1

Related Questions