tags:

views:

67

answers:

3

Hi folks,

this one is really easy.

I'm trying to create a Regular Expression that will result in a Successful Match when against the following text

/default.aspx?

So i tried the following...

^/default.aspx$

and it's failing to match it.

Can someone help, please?

(i'm guessing i'm screwing up becuase of the \ and the ? in the input expression).

+2  A: 

The problem is in the .(dot), which is a wildcard, You must escape it like \..

Also, Because there is a ? at the end of URL and $ (end-of-input) is in the regexp, therefore, it does not match.

The correct regexp should be ^/default\.aspx(\?.*)?$

SHiNKiROU
You need to add \? before the $ or it won't match. The '?' is a reserved character too (meaning zero or 1 of the previous thing) and it should also be escaped with a backslash.
James
ooho. i wasn't tooo far off it. all those brackets stuff always get me stumped, also. can you explain what (?.*) does? i'm guessing the (?.*)? says that stuff in the brackets .. well, it's optional?
Pure.Krome
@Pure: actually, `(?.*)` is a syntax error. It should have been `(\?.*)`: a question mark followed by zero or more of anything. The question mark at the end, as you said, makes the whole group optional.
Alan Moore
+1  A: 

The $ at the end of ^/default.aspx$ means 'match the end of the string', but the string you're searching ends with '?'.

pzr
+1  A: 

Maybe something like this is more appropriate:

^/default\.aspx(\?.*)?$

This will match default.aspx, with an optional ?whatever-else-that-comes-after.

polygenelubricants
Is there any reason to keep the $ at the end of the regex if you are doing something like ".*"?
Iuvat
It depends on the regex implementation because typically '.' does not match new lines.
James
That would explain what I've been missing. I'm usually only matching in single lines.
Iuvat