Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / February 2005

Tip: Looking for answers? Try searching our database.

Questions on a Regular Rexpression

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ioannis Vranos - 25 Feb 2005 21:44 GMT
Given the regular expression:

S"^([a-zA-Z]+|[a-zA-z]+\\s[a-zA-Z]+)$"

1) Isn't the "[a-zA-Z]+|[a-zA-z]+" part redundant? As far as I can
understand it means exactly the same as "[a-zA-Z]+" alone.

2) Isn't the parenthesis grouping redundant?

3) How can we define the parenthesis characters themselves as expected
characters in a match?

Thanks in advance.

Signature

Ioannis Vranos

Carl Daniel [VC++ MVP] - 25 Feb 2005 23:34 GMT
> Given the regular expression:
>
> S"^([a-zA-Z]+|[a-zA-z]+\\s[a-zA-Z]+)$"
>
> 1) Isn't the "[a-zA-Z]+|[a-zA-z]+" part redundant? As far as I can
> understand it means exactly the same as "[a-zA-Z]+" alone.

No, because of the alternative - it's

[a-zA-Z]+

-or-

[a-zA-z]+\\s[a-zA-Z]+

> 2) Isn't the parenthesis grouping redundant?

Since it's the entire expression, yes.  If this expression was embedded
inside a larger regex then no - it defines the limits of the alternative.

> 3) How can we define the parenthesis characters themselves as expected
> characters in a match?

Just escape them:  \\(.  You shouldn't need to escape the right paren in
most cases - just the left.

-cd
Ioannis Vranos - 26 Feb 2005 08:36 GMT
>>1) Isn't the "[a-zA-Z]+|[a-zA-z]+" part redundant? As far as I can
>>understand it means exactly the same as "[a-zA-Z]+" alone.
[quoted text clipped - 6 lines]
>
> [a-zA-z]+\\s[a-zA-Z]+

I did not understand what you mean with the above. May you explain with
some details?

>>2) Isn't the parenthesis grouping redundant?
>
[quoted text clipped - 6 lines]
> Just escape them:  \\(.  You shouldn't need to escape the right paren in
> most cases - just the left.

Ok, thanks for the info.

Signature

Ioannis Vranos

Carl Daniel [VC++ MVP] - 26 Feb 2005 14:36 GMT
>>> 1) Isn't the "[a-zA-Z]+|[a-zA-z]+" part redundant? As far as I can
>>> understand it means exactly the same as "[a-zA-Z]+" alone.
[quoted text clipped - 9 lines]
> I did not understand what you mean with the above. May you explain
> with some details?

The alternative operation has low precendence - lower than concatenation, so

(bob|joe|sue)

parses as 'bob' or 'joe' or 'sue'  not as 'bo'+('b' or 'j')+'o'+('e' or
's')+'ue'

similarly,

[a-zA-Z]+|[a-zA-Z]+\\s+[a-zA-Z]+

parses as

'[a-zA-Z]+' or '[a-zA-Z]+\\s[a-zA-Z]+'

instead of

('[a-zA-Z]+' or '[a-zA-Z]+')\\s+[a-zA-Z]+

does that make sense?

The original expression could be factored, since the alternatives have a
common prefix:

[a-zA-Z]+(\\s+[a-zA-Z]+)?

I would expect a DFA-based regex engine might well do that factoring as a
matter of course when computing the DFA.

-cd
Ioannis Vranos - 26 Feb 2005 15:25 GMT
> The alternative operation has low precendence - lower than concatenation, so
>
[quoted text clipped - 24 lines]
> I would expect a DFA-based regex engine might well do that factoring as a
> matter of course when computing the DFA.

Thanks for the explanation.

Signature

Ioannis Vranos

Serge Baltic - 26 Feb 2005 18:14 GMT
IV> S"^([a-zA-Z]+|[a-zA-z]+\\s[a-zA-Z]+)$"

Note that the [A-z] character set listed above (in the second group) includes
non-alphabetic characters.

Signature

Serge

Ioannis Vranos - 26 Feb 2005 19:20 GMT
> IV> S"^([a-zA-Z]+|[a-zA-z]+\\s[a-zA-Z]+)$"
>
> Note that the [A-z] character set listed above (in the second group)
> includes non-alphabetic characters.

Thanks for the correction, it was just a typo of mine, it was meant to be:

S"^([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)$"

Signature

Ioannis Vranos


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.