Regex Help

J

Jesse Houwing

Hello Barry,
Hi

Using regex i want to extract

games-world-
102a
from http://www.sokoban.com/games/games-world-102a

some times url might be http://www.sokoban.com/games/
or http://www.sokoban.com/games
in the last 2 cases i want "" (empty string) returned

what will the regexpression for the be

TIA
Barry


Regex rx = new Regex(@"http://www\.sokoban\.com/games/(?<captureThis>.*)",
RegexOptions.None);

Match m = rx.Match("... your text here ...")
string result = "";
if (m.Success)
{
result = m.Groups["captureThis"].Value;
}
return result;

You might also want to consider usign the Uri class or System.IO.Path they're
both very good at extractign parts of an URI. And probably a bit faster and
easier to maintain over time.
 

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