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

a simple regex question

Ok, I'm stuck on another Python challenge question. Apparently what you
have to do is search through a huge group of characters and find a
single lowercase character that has exactly three uppercase characters
on either side of it. Here's what I have so far:

pattern = '([a-z][A-Z]{3}[a-z][A-Z]{3}[a-z])+'
print re.search(pattern, mess).groups()

Not sure if 'groups' is necessary or not.

Anyway, this returns one matching string, but when I put this letter in
as the solution to the problem, I get a message saying "yes, but there
are more", so assuming this means that there is more than one character
with three caps on either side, is my RE written correctly to find them
all? I didn't have the parentheses or + sign at first, but I added them
to find all the possible matches, but still only one comes up.

Thanks.
Mar 31 '06 #1
5 1008
John Salerno wrote:
Ok, I'm stuck on another Python challenge question. Apparently what you
have to do is search through a huge group of characters and find a
single lowercase character that has exactly three uppercase characters
on either side of it. Here's what I have so far:

pattern = '([a-z][A-Z]{3}[a-z][A-Z]{3}[a-z])+'
print re.search(pattern, mess).groups()

Not sure if 'groups' is necessary or not.

Anyway, this returns one matching string, but when I put this letter in
as the solution to the problem, I get a message saying "yes, but there
are more", so assuming this means that there is more than one character
with three caps on either side, is my RE written correctly to find them
all? I didn't have the parentheses or + sign at first, but I added them
to find all the possible matches, but still only one comes up.

Thanks.


A quick note: I found nine more matches by using findall() instead of
search(), but I'm still curious how to write the RE so that it works
with search, especially since findall wouldn't have returned overlapping
matches. I guess I didn't write it to properly check multiple times.
Apr 1 '06 #2

John Salerno wrote:
Ok, I'm stuck on another Python challenge question. Apparently what you
have to do is search through a huge group of characters and find a
single lowercase character that has exactly three uppercase characters
on either side of it. Here's what I have so far:

pattern = '([a-z][A-Z]{3}[a-z][A-Z]{3}[a-z])+'
print re.search(pattern, mess).groups()

Not sure if 'groups' is necessary or not.

Anyway, this returns one matching string, but when I put this letter in
as the solution to the problem, I get a message saying "yes, but there
are more", so assuming this means that there is more than one character
with three caps on either side, is my RE written correctly to find them
all? I didn't have the parentheses or + sign at first, but I added them
to find all the possible matches, but still only one comes up.

Thanks.


I don't believe you _need_ the parenthesis or the + in that usage...

Have a look at http://docs.python.org/lib/node115.html

It should be obvious which method you need to use to "find them all"

--
- Justin

Apr 1 '06 #3
Justin Azoff wrote:
John Salerno wrote:
Ok, I'm stuck on another Python challenge question. Apparently what you
have to do is search through a huge group of characters and find a
single lowercase character that has exactly three uppercase characters
on either side of it. Here's what I have so far:

pattern = '([a-z][A-Z]{3}[a-z][A-Z]{3}[a-z])+'
print re.search(pattern, mess).groups()

Not sure if 'groups' is necessary or not.

Anyway, this returns one matching string, but when I put this letter in
as the solution to the problem, I get a message saying "yes, but there
are more", so assuming this means that there is more than one character
with three caps on either side, is my RE written correctly to find them
all? I didn't have the parentheses or + sign at first, but I added them
to find all the possible matches, but still only one comes up.

Thanks.


I don't believe you _need_ the parenthesis or the + in that usage...

Have a look at http://docs.python.org/lib/node115.html

It should be obvious which method you need to use to "find them all"


But would findall return this match: aMNHiRFLoDLFb ??

There are actually two matches there, but they overlap. So how would
your write an RE that catches them both?
Apr 1 '06 #4
John Salerno schreef:
pattern = '([a-z][A-Z]{3}[a-z][A-Z]{3}[a-z])+'
print re.search(pattern, mess).groups()

Anyway, this returns one matching string, but when I put this letter in
as the solution to the problem, I get a message saying "yes, but there
are more", so assuming this means that there is more than one character
with three caps on either side, is my RE written correctly to find them
all? I didn't have the parentheses or + sign at first, but I added them
to find all the possible matches, but still only one comes up.

Thanks.


A quick note: I found nine more matches by using findall() instead of
search(), but I'm still curious how to write the RE so that it works
with search, especially since findall wouldn't have returned overlapping
matches. I guess I didn't write it to properly check multiple times.


It seems to me you should be able to find all matches with search(). Not
with the pattern you mention above: that will only find matches if they
come right after each other, as in
xXXXxXXXxyYYYyYYYyzZZZzZZZz

You'll need something more like
pattern = '([a-z][A-Z]{3}[a-z][A-Z]{3}[a-z]+)+'
so that it will find matches that are further apart from each other.

That said, I think findall() is a better solution for this problem. I
don't think search() will find overlapping matches either, so that's no
reason not to use findall(), and the pattern is simpler with findall();
I solved this challenge with findall() and this regular expression:

pattern = r'[a-z][A-Z]{3}[a-z][A-Z]{3}[a-z]'
--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven
Apr 1 '06 #5

John Salerno wrote:
But would findall return this match: aMNHiRFLoDLFb ??

There are actually two matches there, but they overlap. So how would
your write an RE that catches them both?


I remembered the 'non-consuming' match (?+...) and a miniute of
experimentation gave
the following.
import re
s ="aMNHiRFLoDLFb"
re.findall(r'[A-Z]{3}([a-z])(?=[A-Z]{3})', s) ['i', 'o']


- Paddy.

Apr 2 '06 #6

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

Similar topics

3
by: Howard Dean | last post by:
I have a string with several carriage returns in it. For example: "This is my test string" I wish to convert it to "This is my test string" (remove all carriage returns. Can someone tell me...
2
by: K. Shier | last post by:
my regex for matching phone #'s: \({0,1}(?<areacode>{3})\){0,1}( |-){0,1}(?<prefix>{3})( |-){0,1}(?<suffix>{4}) which i then .Replace to become: (${areacode}) ${prefix}-${suffix} question: ...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
20
by: Larry Woods | last post by:
I'm drawing a blank... What is the regular expression for search a string for NO occurances of a substring? Example: I want to find all lines that do NOT have the substing "image" in them. ...
5
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated...
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
2
by: Warren | last post by:
Sorry to ask something so elementary, but I'm just not getting it. suppose I want to match the value of parameter b from a URL like http://www.abcd.com/?a=1&b=2&c=3 I can use this regex...
4
by: bg_ie | last post by:
I have the following Regex - oldString = @"C:\ta"; Regex regexEndsInBackslash = new Regex(oldString + @"\\", RegexOptions.Singleline | RegexOptions.IgnoreCase); Any ideas why none of the...
4
by: MaxMax | last post by:
I want to add a text to another text using regular expressions... An example before= "foo" after= "foobar" before= "one" after= "onebar" before = "" after = "bar"
2
by: Sin Jeong-hun | last post by:
I'm trying to replace \n with @(representing the actual new line character). Of course, \\n should not be replaced. What I'm trying to do is simply what the good-old printf does. For example, "\\n...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.