[C#] Why does this regex not work?

Coron

Newbie
Joined
Jan 13, 2012
Messages
22
Reaction score
0
I'm pretty tired and I've probably missed something, but why does the regex below not work?
Code:
            Regex captchaRegex = new Regex("src=\"example/Captcha?ctoken=(.*?)\"");
            Match captchaMatch = captchaRegex.Match("<img src=\"example/Captcha?ctoken=Get this\" width=\"200\" height=\"70\" alt=\"Visual verification\">");

In case you're wondering, I'm trying to get the captcha image on Google. It doesn't match, I don't get it!!

Also I had to replace an URL in the code with 'example' because I'm new.
 
Try this regex
<img.+?src=[\"']example/Captcha\?ctoken=(.+)?[\"']>
 
try this updated
<img.+?src=[\"']example/Captcha\?ctoken=(?<val>.+)?[\"'] width=["](.*)?["] height=["](.*)?["] alt=["](.*)?["]>

then you can get the value using captchaMatch.Groups["val"].Value
 
Try this regex
<img.+?src=[\"']example/Captcha\?ctoken=(.+)?[\"']>

Thanks for the reply. After changing example to my URL it didn't work. I'm very unfamiliar with regex and I'm not sure if I need to add brackets to the URL as well?

Here is the full regex:
src="hxxxx://accounts.google.xxx/Captcha?ctoken=(.*?)"

edit: I'll try your new reply
 
try this updated one for actual source
<img.+?src=[\"'].*?\?ctoken=(?<val>.+)?[\"'] width=["](.*)?["] height=["](.*)?["] alt=["](.*)?["]>

captchaMatch.Groups["val"].Value
 
Code:
ResultString = Regex.Match(SubjectString, "ctoken=(.*?)\\\"").Groups[1].Value;
 
is the captcha number showing up on the image url , if it really the case it will be really dumb from google and i guess that where you have a problem with your regex.The value might be showing up in your browser but it not really the case.
 
Look into RegexBuddy. One of the best $30 I ever spent. The time it saves you in learning and debugging regex is amazing!
 
Code:
  Regex captchaRegex = new Regex("Captcha[?]ctoken=(.+?)\"");
            Match captchaMatch = captchaRegex.Match("<img src=\"example/Captcha?ctoken=Get this\" width=\"200\" height=\"70\" alt=\"Visual verification\">");
            string getThis = captchaMatch.Groups[1].Value;

? is a regex operator, you should escape it with []
 
Try something like this (Second result, haven't tested against yours string)

[\<]img[\ ]src[\=][\"]([^\=]*)[\=]([^\"]*)[\"][\ ]width[\=][\"]([^\"]*)[\"][\ ]height[\=][\"]([^\"]*)[\"][\ ]alt[\=][\"]([^\"]*)[\"][\>]
 
Back
Top