473,799 Members | 3,310 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regular expressions

Hi guys,

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

System.Text.Reg ularExpressions .Match match =
System.Text.Reg ularExpressions .Regex.Match(re questPath, "([^/]*?\
\.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 2760
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.Reg ularExpressions .Match match =
System.Text.Reg ularExpressions .Regex.Match(re questPath, "([^/]*?\
\.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">Cli ck 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.goo glegroups.com.. .
Hi guys,

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

System.Text.Reg ularExpressions .Match match =
System.Text.Reg ularExpressions .Regex.Match(re questPath, "([^/]*?\
\.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...@n othinks.comwrot e:
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">Cli ck 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.goo glegroups.com.. .
Hi guys,
I need some help regarding regular expressions. Consider the following
statement :
System.Text.Reg ularExpressions .Match match =
System.Text.Reg ularExpressions .Regex.Match(re questPath, "([^/]*?\
\.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
2433
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: 0.04, 0.02, 0.01" how can I extract this number with RE or otherwise?
1
4187
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 regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
2
5100
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 have to use all the expressions seperately? Here are my regular expressions that check for valid email address and link Dim Expression As String =
4
5187
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 adate LIKE "2004.01.10 __:15") .... into something like this: .... where adate LIKE "2004.01.10 __:(30/15)" ...
7
3831
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 want to avoid that. My question here is if there is a way to pass either a memory stream or array of "find", "replace" expressions or any other way to avoid multiple copies of a string. Any help will be highly appreciated
3
3027
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 Expressions used in Perl identical to the Regular Expressions in PHP?
25
5174
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 (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access to the expression (not the matches) at runtime? Thanks, Mike
1
4388
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 the first regular expression that matches the string. I've gor the regular expressions ordered so that the highest priority is first (if two or more regular expressions match the string I want the first one returned) The code that does this has...
13
7495
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 be accomplished with regular expressions this way, such as validating a mathematical expression or parsing a language with nested parens, quoting or expressions. Another feature I'm missing is once-only subpatterns and possessive quantifiers...
12
2493
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 http://en.wikipedia.org/wiki/Regular_expression http://www.regular-expressions.info/javascript.html http://www.webreference.com/js/column5/
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10485
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10252
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.