473,386 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Regular expressions

Hi guys,

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

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

(where requestPath is a string)

What does the regex: [^/]*?\\.ashx actually do ? How come * and ?
occur consecutively ?
Doesn't '?' require some text/block of text before it ?
Also, 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 !

Mar 7 '07 #1
3 2724
Oops, I guess that should go to the CSharp forum, but do let me know
if you can help me.

Thanks !

On Mar 7, 5:34 pm, "Zeba" <coolz...@gmail.comwrote:
Hi guys,

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

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

(where requestPath is a string)

What does the regex: [^/]*?\\.ashx actually do ? How come * and ?
occur consecutively ?
Doesn't '?' require some text/block of text before it ?
Also, 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 !

Mar 7 '07 #2
Regular Expressions are a powerful way to match patterns of characters in
strings.

The Regular Expression engine is basically procedural in nature, examining a
string one character at a time, but although it moves from left to right
through the string, it has the capability to move (jump) backwards as well,
and to keep track of multiple matches, groups, and so on.

What it does is to use a syntax that identifies sequences of characters in a
string. In your example,

[^/]*?

is essentially what is called a "character class." A character class is a
set of matching characters which can appear in any order, and a match can
contain any of the characters. The characters in the set are identified by
the [square brackets] surrounding them. The character '^' indicates a "NOT"
grouping, which means that a match may NOT contain any of the characters in
the set. The '/' character is the only character in this particular set.

The character following the character class is a quantifier. It indicates
how many characters in the set constitute a match. The '*' character
signifies "zero or more." Some other quantifiers are '+' (one ore more), '?'
(zero or one), and sets of numbers in curly brackets, for example: {2}
(exactly 2), {1,5} (between 1 and 5 inclusive).

The '?' following the '*' in this case is NOT a quantifier. It is determined
by its' context in the pattern. If it immediately followed the character
class it would be a quantifier, but because it follows the quantifier, it
modifies the quantifier. It indicates that the character set is "lazy" as
opposed to "greedy." This is a little harder to explain. Regular Expressions
are "greedy" by default. That is, if a string contains a continuous set of
characters that constitute a match, followed by one or more continuous
characters that constitute a match, the matches are combined into a single
match, for as many times as there are sets of continuous matching
characters.

For example, if you are looking for an HTML tag in a document, you might
think the following would work:

<.+(a left angle bracket, followed by any non-line-break character one or
more times, followed by a right angle bracket)

If you were looking at the following HTML:

<a href="blah">Click Here</a>

You might think that it would capture the opening tag. However, it would
capture the entire string. Why? Because the right angle-bracket in the
opening tag is not a line-break character. Yes, the match MUST end in a
right angle bracket. However, since RegEx is greedy, it will continue until
it finds a character that does NOT match the expression.

If you were to use the following instead:

<.+?>

It would stop at the first right-angle bracket. This is because the '?'
means that as few non-line-break characters as possible should match before
the right angle bracket.

You could also do the following:

<[^>]+>

This means that any right angle bracket character can not be part of the
match prior to the right angle bracket at the end of the match.

Here's a good reference on using Regular Expressions with the .Net platform:

http://msdn2.microsoft.com/en-us/library/hs600312.aspx

--
HTH,

Kevin Spencer
Microsoft MVP

Help test our new betas,
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Zeba" <co******@gmail.comwrote in message
news:11**********************@t69g2000cwt.googlegr oups.com...
Hi guys,

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

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

(where requestPath is a string)

What does the regex: [^/]*?\\.ashx actually do ? How come * and ?
occur consecutively ?
Doesn't '?' require some text/block of text before it ?
Also, 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 !

Mar 7 '07 #3
Thanks ! That was very helpful.

On Mar 7, 6:17 pm, "Kevin Spencer" <unclechut...@nothinks.comwrote:
Regular Expressions are a powerful way to match patterns of characters in
strings.

The Regular Expression engine is basically procedural in nature, examining a
string one character at a time, but although it moves from left to right
through the string, it has the capability to move (jump) backwards as well,
and to keep track of multiple matches, groups, and so on.

What it does is to use a syntax that identifies sequences of characters in a
string. In your example,

[^/]*?

is essentially what is called a "character class." A character class is a
set of matching characters which can appear in any order, and a match can
contain any of the characters. The characters in the set are identified by
the [square brackets] surrounding them. The character '^' indicates a "NOT"
grouping, which means that a match may NOT contain any of the characters in
the set. The '/' character is the only character in this particular set.

The character following the character class is a quantifier. It indicates
how many characters in the set constitute a match. The '*' character
signifies "zero or more." Some other quantifiers are '+' (one ore more), '?'
(zero or one), and sets of numbers in curly brackets, for example: {2}
(exactly 2), {1,5} (between 1 and 5 inclusive).

The '?' following the '*' in this case is NOT a quantifier. It is determined
by its' context in the pattern. If it immediately followed the character
class it would be a quantifier, but because it follows the quantifier, it
modifies the quantifier. It indicates that the character set is "lazy" as
opposed to "greedy." This is a little harder to explain. Regular Expressions
are "greedy" by default. That is, if a string contains a continuous set of
characters that constitute a match, followed by one or more continuous
characters that constitute a match, the matches are combined into a single
match, for as many times as there are sets of continuous matching
characters.

For example, if you are looking for an HTML tag in a document, you might
think the following would work:

<.+(a left angle bracket, followed by any non-line-break character one or
more times, followed by a right angle bracket)

If you were looking at the following HTML:

<a href="blah">Click Here</a>

You might think that it would capture the opening tag. However, it would
capture the entire string. Why? Because the right angle-bracket in the
opening tag is not a line-break character. Yes, the match MUST end in a
right angle bracket. However, since RegEx is greedy, it will continue until
it finds a character that does NOT match the expression.

If you were to use the following instead:

<.+?>

It would stop at the first right-angle bracket. This is because the '?'
means that as few non-line-break characters as possible should match before
the right angle bracket.

You could also do the following:

<[^>]+>

This means that any right angle bracket character can not be part of the
match prior to the right angle bracket at the end of the match.

Here's a good reference on using Regular Expressions with the .Net platform:

http://msdn2.microsoft.com/en-us/library/hs600312.aspx

--
HTH,

Kevin Spencer
Microsoft MVP

Help test our new betas,
DSI PrintManager, Miradyne Component Libraries:http://www.miradyne.net

"Zeba" <coolz...@gmail.comwrote in message

news:11**********************@t69g2000cwt.googlegr oups.com...
Hi guys,
I need some help regarding regular expressions. Consider the following
statement :
System.Text.RegularExpressions.Match match =
System.Text.RegularExpressions.Regex.Match(request Path, "([^/]*?\
\.ashx)");
(where requestPath is a string)
What does the regex: [^/]*?\\.ashx actually do ? How come * and ?
occur consecutively ?
Doesn't '?' require some text/block of text before it ?
Also, 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 !

Mar 8 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Michael McGarry | last post by:
Hi, I am horrible with Regular Expressions, can anyone recommend a book on it? Also I am trying to parse the following string to extract the number after load average. ".... load average:...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
2
by: Sehboo | last post by:
Hi, I have several regular expressions that I need to run against documents. Is it possible to combine several expressions in one expression in Regex object. So that it is faster, or will I...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
3
by: a | last post by:
I'm a newbie needing to use some Regular Expressions in PHP. Can I safely use the results of my tests using 'The Regex Coach' (http://www.weitz.de/regex-coach/index.html) Are the Regular...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
13
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can...
12
by: FAQEditor | last post by:
Anybody have any URL's to tutorials and/or references for Regular Expressions? The four I have so far are: http://docs.sun.com/source/816-6408-10/regexp.htm...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.