Reputation: 13
I need some help from someone with more knowledge in Regular Expressions than I have. My problem is want to POST a form, but need a 'reloadToken' for that, it's required to be posted. The reloadToken can be found in the page source in a hidden value, for example:
<input type="hidden" name="reloadToken" value="80c52d77d3e2eeb32fac3a6940ab6cc4" />
The value of the reloadToken changes every pageload, I'm now sending a GET request and save the source in a variable, with regex I should be able to search the source for the reloadToken, but my knowledge of regex is near to zero.
I hope someone can help me using regex.
There's one other thing, there are more reloadToken's in the pagesource, however they all have the same value, so it shouldn't matter right?
Thanks in advance.
Upvotes: 1
Views: 576
Reputation: 31616
What the other answers are forgetting is that you are working in C# which has its own compiler escapes
which most are mirrored in Regex.
Their patterns may work but you will be fighting the C# parsers escapes of the double quotes before it even gets to regex and it may harm the pattern which will cause a failure.
Below in my pattern I show how to use the literal escape @
in C# to send an unadulterated text pattern to the parser in C#:
string data="<input type=\"hidden\" name=\"reloadToken\" value=\"80c52d77d3e2eeb32fac3a6940ab6cc4\"";
string pattern=@"(?:reloadToken.+value="")(?<Token>[^\""]+)";
Console.WriteLine (
Regex.Match(data, pattern)
.Groups["Token"]
.Value);
// Output
// 80c52d77d3e2eeb32fac3a6940ab6cc4
Otherwise the pattern says
(?: ... )
- Match but don't capture a specific set of text. The text we don't need to capture or match it, it is simply anchor text; see next...reloadToken.+value=""
- Actual text "reload token" then we are going to eat up generic space by specifying the .
which says match anything. But we want to match more than one so we add a +
which means 1 to many times. So space will be eaten up to a literal value="
.
(?<Token> ...)
- The (
to )
says this is a match group. The ?<xxx>
names the match group. In our case our match group will be named "Token" which will allow us to extract it from a match capture group
(regex term) later by that name instead of an index.
[^\""]+
- [
to ]
says this is a set definition. In our set definition we say ^
not any "
quotes. So we want to match anything but a quote and finally we say our match group can be matched +
1 or more characters. Which will stop at the first "
.
Upvotes: 1
Reputation: 2376
This should work for you. The value you are looking to get is a capture group, while the rest is not.
(?:name="reloadToken" value=")([^"]*)
Upvotes: 0