LeMoussel
LeMoussel

Reputation: 5767

C# Regex to match Url folder pattern

I have an application which needs to find and then process Urls which follow a pattern like this: http://www.actuino.fr/projets/frankenblink http://www.actuino.fr/projets/ http://www.actuino.fr/projets

I have the following pattern which almost works...

string pattern = @"http://www.actuino.fr/projets/?.*";

Unfortunately that pattern will grab all Urls with 'projets' in as like this

http://www.actuino.fr/projetsarduino http://www.actuino.fr/projets_rasberry

Thank you for your time.

Upvotes: 1

Views: 118

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174696

Use word boundary.

string pattern = @"http://www\.actuino\.fr/projets\b/?.*";

or

Positive lookahead Assertion.

string pattern = @"(?m)http://www\.actuino\.fr/projets(?=/|$)/?.*";

(?=/|$) asserts that the previous token projects must be followed by a / or end of the line.

Upvotes: 3

Related Questions