Reputation: 3374
I want to find exact url mach in url list using with Regular Expression .
string url = @"http://web/P02/Draw/V/Service.svc";
string myword = @"http://web/P02/Draw/V/Service.svc http://web/P02/Draw/V/Service.svc?wsdl";
string pattern = @"(^|\s)" + url + @"(\s|$)";
Match match = Regex.Match(pattern, myword);
if (match.Success)
{
myword = Regex.Replace(myword, pattern, "pattern");
}
But the pattern returns no result.
What do you think is the problem ?
Upvotes: 1
Views: 704
Reputation: 50114
.
in a regular expression pattern is a special character, so you need to escape url
when you use it to build pattern - you can use Regex.Escape(url)
Upvotes: 0
Reputation: 27
Strange formatting aside, here is a pattern to match each individual URL in your list.
Pattern = "http://([a-zA-Z]|/|[0-9])*\.svc";
Frankly, I don't think you're having issues with syntax or implementation. If you want to tweak the expression I wrote above, this is the place to do it: Online RegEx Tool
Upvotes: 2
Reputation: 6047
Why not use Linq on the string collection (when splitted by a space)
myword.Split(' ').Where(x => x.Equals(url)).Single().Replace(url, "pattern");
Upvotes: 1
Reputation: 9566
You're passing wrong arguments to Regex.Match
method. You need to swap arguments like this>
Match match = Regex.Match(myword,pattern);
Upvotes: 1