Gregory Nozik
Gregory Nozik

Reputation: 3374

Find exact url match

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

Answers (4)

Rawling
Rawling

Reputation: 50114

  • You've got your arguments the wrong way around, as has been pointed out
  • . 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)
  • You don't need to check the match is a success before performing the replacement, unless you have other logic that depends on whether the match was a success.

Upvotes: 0

ProfessorY
ProfessorY

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

JP Hellemons
JP Hellemons

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

RePierre
RePierre

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

Related Questions