Link to home
Start Free TrialLog in
Avatar of jay_eire
jay_eireFlag for United States of America

asked on

Regular Expression to match exact match a URL

Hi,
I am trying to write a regular expression that will exact match against URL:
http://student-intranet.registration:2013/RegNow/complete/confirmation.html 

I put together this [^]/RegNow/complete/confirmation.html[^]

But when running the regular expression it matches against other URLS:
http://student-intranet.registration:2013/RegNow/regformreview.html
http://student-intranet.registration:2013/RegNow/complte/processing
http://student-intranet.registration:2013/RegNow/regform.html
http://student-intranet.registration:2013/RegNow/complete/confirmation.html 

What should I change in the syntax for an exact match?
[^]/RegNow/complete/confirmation.html[^]
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jay_eire

ASKER

Thanks Kaufmed, can I ask what \ and $ do exactly?

Also was I wrong in using [^]
The \ escapes the dot. Dot (unescaped) in regex means "any character". While dot would still match the dot within the filename, it's more correct to explicitly escape the dot when you actually mean to locate a dot.

The $ means "end of string." It's not an actual character, but rather a place within the string. Similarly ^ means "start of string."

Your use of [^], in contradiction to what I just described with respect to ^ is actually something different. Square brackets ( [] ) within regex denote a "character class," which is a listing of characters that you would like any of to match. If I gave you "[abc]", for example, then I would be saying that I want to match either an "a", "b", or "c" character. But not all. Only one of the characters listed, in this example, should match within the target string. This is due to how I wrote the character class. There are ways to make it match more than one.

I am not certain that [^] will compile as a valid expression, however. There is one more peculiarity with ^ and square brackets. Above I mentioned that [abc] would match any single instance of either of those characters. If I include a ^ in this manner:

    [^abc]

...then I am saying that I want to match any character that is not ( ^ ) an "a", "b", or "c". ^ as the first character of a character class negates the character class, and causes the character class to mean "any character not one of the ones listed." I cannot immediately recall if [^] is a valid expression or not. Nevertheless, I do not thing that [^] was necessary for your pattern.

P.S.

In order to say "any character not a ^," you would actually do:

    [^^]
Thank you for the solution and explanation.