473,385 Members | 1,531 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,385 software developers and data experts.

Questions on a Regular Rexpression

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.

--
Ioannis Vranos
Nov 17 '05 #1
6 957
Ioannis Vranos wrote:
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
Nov 17 '05 #2
Carl Daniel [VC++ MVP] wrote:
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]+

I did not understand what you mean with the above. May you explain with
some details?
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.

Ok, thanks for the info.

--
Ioannis Vranos
Nov 17 '05 #3
Ioannis Vranos wrote:
Carl Daniel [VC++ MVP] wrote:
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]+

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

Nov 17 '05 #4
Carl Daniel [VC++ MVP] wrote:
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.

Thanks for the explanation.

--
Ioannis Vranos
Nov 17 '05 #5
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.

--
Serge

Nov 17 '05 #6
Serge Baltic wrote:
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]+)$"

--
Ioannis Vranos
Nov 17 '05 #7

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

Similar topics

1
by: Jeremy | last post by:
I am (very) new top regular expressions and I am having a difficult time understanding how to do them. I have the following in my script: zaidsearch = r'''^ {5,}({4,5})(.\d{2,2}c)''' ZAIDSearch...
0
by: Atip Asvanund | last post by:
Dear sirs, I am trying to learn how to use Boehm's garbage collector: http://www.hpl.hp.com/personal/Hans_Boehm/gc/ on a Linux machine. I am a beginner, and I find its documentation inadequate....
7
by: Johnny | last post by:
June 2, 2005 Greetings, I have a whole lot of questions that all have to do with the same topic: Width. By "width" I mean how wide a particular Web page is, how wide a table is, how wide a...
1
by: Fernando | last post by:
Hello, i've been reading the README file in the lmgr folder (in src/backend/storage/lmgr/) and i have a couple of questions about locks: - What's the difference between the Lightweight Locks...
15
by: S.Tobias | last post by:
In the past weeks I asked two questions concerning unions, and I got not a single reply to either. What should I do next? Re-post them? Ask them again in csc? -- Stan Tobias sed 's///g' to...
102
by: dreamznatcher | last post by:
Hello, I'm considering a career switch to a more database-related job, but need help on a few questions and issues. I'm a Computer Engineering graduate and have always felt most comfortable...
22
by: gustum | last post by:
Im graduating in the coming december. Anyone pls guide me where i can get c++ interview questions. I shall be very thankful to you if you provide me good link so any stuff if you have regarding...
8
by: Krypto | last post by:
Hi, I have used Python for a couple of projects last year and I found it extremely useful. I could write two middle size projects in 2-3 months (part time). Right now I am a bit rusty and trying...
0
by: George Orwell | last post by:
fread breaks file descriptors opened in "w" mode. ... Yes, once you break out of ANSI/ISO C, all things are possible. You have posted an on-topic question to comp.lang.c. See version 2.0 of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.