473,856 Members | 1,577 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 2764
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
2437
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
4193
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
5117
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
5197
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
3836
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
3032
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
5192
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
4392
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
7502
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
2499
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
9765
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10700
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
10795
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9536
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7934
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
5763
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4581
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
4177
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3203
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.