Regex Help

  • Thread starter Thread starter Barry
  • Start date Start date
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.
 
Back
Top