Regular expressions

Z

Zeba

Hi guys,

I need some help regarding regular expressions. Consider the following
statement :

System.Text.RegularExpressions.Match match =
System.Text.RegularExpressions.Regex.Match(requestPath, "([^/]*?\
\.ashx)");

(where requestPath is a string)

1. What does the regex:
[ ^ / ] * ? \ \ . ashx
(no spaces between the special characters in actual expression)
actually do ? How come * and ? occur consecutively ?
2. Doesn't '?' require some text/block of text before it ?
3. Is the expression read left to right or right to left ?
i.e. is the backslash grouped as ' \ \ ' . or \ ' \ .' ? If it is
the
former, why is it not written as \ \ \ . and if latter what does the
orphaned backslash do ?

Hope that's not too many questions - I'm too confused !

Thanks !
 
P

Peter Bradley

The question mark after the asterisk specifies a non-greedy match.

Because the expression preceding the question mark could potentially match
an entire string ending .ashx, the question mark makes the regular
expression parser go no further than the first backshlash. Matches are
greedy by default.



Peter
 
R

Richard Lewis Haggard

I'm not great with regular expressions, but here's my interpretation.

The square brackets define a range of characters. Normally, the match is
anything within the range. In this case, the range starts with a '^' which
negates the range, turning the match into anything that is NOT in the range.
The range itself is a single character, a forward slash.

The asterisk indicates 'zero or more' of the preceding pattern. Normally, *
will match the most it can. The following '?' normally means match 0 or 1
times, but in this case, because it follows an asterisk, it modifies the
meaning of the asterisk so that it is a 'lazy' match, so that it matches the
least it can get away with. In other words, a single forward leaning slash
constitutes a match.

The double left leaning slash means a slash literal is next, followed by
'.ashx'.

Taking this altogether, given "/abc/def/\.ashx" then "/\.ashx" is the
matching string.
 
Z

Zeba

What if the string were "/abc/def/ghi\mashx"...Then the matching
string should be ghi\mashx, right (since period stands for any
character) ? If the intention of the regex is to catch the name of the
ashx application, shouldn't it actually be written as

[ ^ / ] * ? \ . ashx

Thanks !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top