Regular expressions: Show matches [modified]
-
Hello C# programmers, I currently dabble in programming with regular expressions. My problem is that I don't know, how I can extract a match if the programm finds one in a string. I already googled, but I couldn't found a site that could explain me how to manage it. I already tried
Match
andMatchCollection
but it did not work. My previous source is following:string test = "gjhsdjfjhfsdfjhsdf\ntztrzrtz\n\"http://screenshots.filesnetwork.com/68/potd/t\_1215615627\_85.jpg\\"\\nggdgfdg56ggdg";
string pattern = @"|.+\""http\://screenshots\.filesnetwork\.com/68/potd/t_[0-9]+_[0-9]+\.jpg\"".+|""";
Regex re = new Regex(pattern,RegexOptions.Multiline);
I am helpless with the rest, even though I know that it won't be that difficult. I'm looking forward to your answers. :) Best regards MyPiano
modified on Sunday, August 3, 2008 8:52 AM
-
Hello C# programmers, I currently dabble in programming with regular expressions. My problem is that I don't know, how I can extract a match if the programm finds one in a string. I already googled, but I couldn't found a site that could explain me how to manage it. I already tried
Match
andMatchCollection
but it did not work. My previous source is following:string test = "gjhsdjfjhfsdfjhsdf\ntztrzrtz\n\"http://screenshots.filesnetwork.com/68/potd/t\_1215615627\_85.jpg\\"\\nggdgfdg56ggdg";
string pattern = @"|.+\""http\://screenshots\.filesnetwork\.com/68/potd/t_[0-9]+_[0-9]+\.jpg\"".+|""";
Regex re = new Regex(pattern,RegexOptions.Multiline);
I am helpless with the rest, even though I know that it won't be that difficult. I'm looking forward to your answers. :) Best regards MyPiano
modified on Sunday, August 3, 2008 8:52 AM
-
Thanks for the answer. The tool is great, but it does not tell me, how I can extract the Matches. The new pattern:
string pattern = ".+(\"http\\://screenshots\\.filesnetwork\\.com/68/potd/t_[0-9]+_[0-9]+\\.jpg\").+";
By the way, the real string I am searching in looks so:
...
src="http://screenshots.filesnetwork.com/68/potd/t\_1215615627\_85.jpg" alt="Knights of the Old Republic 2" width="160" height="120"
...
EDIT: I made a little progress:
Match m = re.Match(webSource);
richTextBox1.Text = m.Value;I get an extract from the whole html document now, but it is still to big. All I want to extract is this part, which my pattern shell find:
http://screenshots.filesnetwork.com/68/potd/t\_1215615627\_85.jpg
EDIT II: I think I got it now. The mistake was the
.+
at the start and at the end. If someone finds more mistakes or has tipps for me, I would be glad to read them. :) Best regards Joris -
Thanks for the answer. The tool is great, but it does not tell me, how I can extract the Matches. The new pattern:
string pattern = ".+(\"http\\://screenshots\\.filesnetwork\\.com/68/potd/t_[0-9]+_[0-9]+\\.jpg\").+";
By the way, the real string I am searching in looks so:
...
src="http://screenshots.filesnetwork.com/68/potd/t\_1215615627\_85.jpg" alt="Knights of the Old Republic 2" width="160" height="120"
...
EDIT: I made a little progress:
Match m = re.Match(webSource);
richTextBox1.Text = m.Value;I get an extract from the whole html document now, but it is still to big. All I want to extract is this part, which my pattern shell find:
http://screenshots.filesnetwork.com/68/potd/t\_1215615627\_85.jpg
EDIT II: I think I got it now. The mistake was the
.+
at the start and at the end. If someone finds more mistakes or has tipps for me, I would be glad to read them. :) Best regards JorisThe Value property of the Match object contains the entire string that was matched. The Groups collection contains the values from the groups that you specified in the pattern, in this case the group matching the url.
Despite everything, the person most likely to be fooling you next is yourself.
-
The Value property of the Match object contains the entire string that was matched. The Groups collection contains the values from the groups that you specified in the pattern, in this case the group matching the url.
Despite everything, the person most likely to be fooling you next is yourself.