-2
"member.php?1-ricarod&amp;s=50793b6188295f764bc938b1737b0a18" class="avatarlink"><img src="images/misc/unknown.gif" class="userlist_avatar_1" alt="" border="0" 

I need to parse ricarod from it. I try code

name = Regex.Match(content, @"\.php?1-(.*?)&.+"".class=""avatarlink").Groups[1].Value;

but it doesn't work. Why?

Cœur
  • 37,241
  • 25
  • 195
  • 267
broadbandninja
  • 81
  • 1
  • 1
  • 4
  • 1
    wait..is that `html`.. **don't** parse it with regex..use an html parser..also please show us the complete input..atleast a node.. – Anirudha Feb 07 '13 at 18:26
  • http://pastebin.com/yJx5sLN6 – broadbandninja Feb 07 '13 at 18:28
  • @Some1.Kill.The.DJ - That's a knee jerk reaction. The OP is not trying to parse HTML. http://stackoverflow.com/a/4231482/211627 – JDB Feb 07 '13 at 18:32
  • @Cyborgx37 its better to use `parser` to collect all `anchor` tags and then parse the value with `regex`..that's the right technique..using that `regex` over the entire `html` is not only **inefficient** but can also cause an **error**.. – Anirudha Feb 07 '13 at 18:37
  • 1
    @Some1.Kill.The.DJ - You mean that simply iterating over text is less efficient then parsing it into a DOM? I'd like to see something to back that up (especially given how blazingly fast modern regex engines are). – JDB Feb 07 '13 at 18:44
  • @Cyborgx37 it's also about getting the right value..what if the same pattern is repeated somewhere he never expected it to be like in user input..although the probability of this happening is less but y to take risk.. – Anirudha Feb 07 '13 at 19:07
  • @Some1.Kill.The.DJ - I would think the probability would be very low, not worth all the additional effort, but that's up to the OP. But regex is perfectly functional for what the OP has said s/he wants to do (the URL being in an anchor tag is never stated as a requirement). – JDB Feb 07 '13 at 19:13

3 Answers3

1

You forgot to escape a ?

name = Regex.Match(content, 
    @"\.php\?1-(.*?)&.+"".class=""avatarlink").Groups[1].Value;
FlyingStreudel
  • 4,434
  • 4
  • 33
  • 55
0

Try escaping the question mark after "php". I don't guarantee that'll fix it, but that's probably part of the problem.

There's a good online regex tester at regexlib.com

Ann L.
  • 13,760
  • 5
  • 35
  • 66
0

The question mark has special meaning for regular expressions. Try

 var name = Regex.Match(content, @"\.php\?1-(.*?)&").Groups[1].Value; 

Note that the '?' is escaped. Also, if you're only interested in 'ricardo', you can simplify your regex as well.

Reacher Gilt
  • 1,813
  • 12
  • 26