2

I have the following string (from a large HTML string):

href="/cgi-bin/pin.cgi?pin=94841&sid=9548.1386389012.v1"><

And here is my code:

var sids = Regex.Matches( htmlCode, "sid=(.)\">" );

I'm not pulling back any results. Is my Regex correct?

0

3 Answers 3

1

This is what it should be:

var str = @"href=""/cgi-bin/pin.cgi?pin=94841&sid=9548.1386389012.v1"">";
var sid = Regex.Match(str, @"sid=([^""]*)");
Console.WriteLine (sid.Groups[1].Value);

What you originally posted was wrong because "." acts as a wildcard, and the way you presented it meant that it would only capture 1 character, the problem with wildcards is that they're difficult to stop till you reach the end of a line, so never use them unless you have to.

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

Comments

0

. match only single character. To match multiple character you should use * or + modifier: (.+); or more preferably non-greedy version: (.+?)

Use @"verbatim string literal" if possible for regular expression.

var sids = Regex.Matches(htmlCode, @"sid=(.+?)""");

See demo run.

Comments

0

I think you are pretty close. Consider the following minor change to your regex...

sid=.*?\">

Good Luck!

Comments

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.