Regular Expression Blues

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
 
U

UL-Tomten

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top