1

In a C# project, Regex is behaving weirdly for me.

I have this method:

string RegTest()
    {
        string HTML = "<input type=\"hidden\" name=\"authenticity_token\" value=\"d27956cca6b75db4d8dd502d0569dd246455131c\">";
        Regex AuthRegex = new Regex(@"name=""authenticity_token"" value=""([A-Ba-b0-9/-]+)""");
        string Auth = AuthRegex.Match(HTML).Value;
        return Auth;
    }

For a reason I don't understand at all, the Regex doesn't find any match with this pattern. It just returns "".

How can I fix this?

1
  • @ndn I thought that by using @ I don't need to escape it. What is the correct way to escape it in the pattern? Commented Aug 29, 2015 at 12:18

1 Answer 1

2

The problem is:

[A-Ba-b0-9/-]+

What character ranges (x-y) basically do is get a set of all characters in between. In other words, a-b = all letters between a and b, aka only a and b. However,

d27956cca6b75db4d8dd502d0569dd246455131c

looks like a hex. Therefore, you should use

[A-Fa-f0-9-]+

instead.

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

1 Comment

OK, Thank you very much! And now I've also noticed I wrote A-B instead of writing A-Z like I usually do... -_-

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.