Regular Expression Blues

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

I need a regular expression that will give me a List<string> of all URLs it
finds within a piece of text I provide. I know that all of the URLs will
begin with something like:
https://www.somesite.com/

I really stink at RegEx - can someone help me out? I'm also not sure how to
have it actually return a List...

TIA
Brian
 
I really stink at RegEx - can someone help me out?

You're in luck! Regular expression tutorials is the second most common
type of web site on the Internet.
I'm also not sure how to have it actually return a List...

It won't; you'll have to construct the list yourself, along the lines
of:

List<string> l = new List<string>();
foreach (Match m in Regex.Matches(s, "http://[0-9?&a-z-_./]
+", RegexOptions.Singleline)) {
l.Add(m.Value);
}

This uses the static method Regex.Matches, which essentially
constructs a RegEx instance and discards it after finding the matches.
You might want to reuse the RegEx instance for performance reasons.
 
Back
Top