473,473 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

regex problem

Input is a string of four digit sequences, possibly
separated by a -, for instance like this

"1234,2222-8888,4567,"

My regular expression is like this:

rx1=re.compile(r"""\A(\b\d\d\d\d,|\b\d\d\d\d-\d\d\d\d,)*\Z""")

When running rx1.findall("1234,2222-8888,4567,")

I only get the last match as the result. Isn't
findall suppose to return all the matches?

Thanks in advance.
--
Har du et kjøleskap, har du en TV
så har du alt du trenger for å leve

-Jokke & Valentinerne
Jul 26 '05 #1
5 989
Am Tue, 26 Jul 2005 09:57:23 +0000 schrieb Odd-R.:
Input is a string of four digit sequences, possibly
separated by a -, for instance like this

"1234,2222-8888,4567,"

My regular expression is like this:

rx1=re.compile(r"""\A(\b\d\d\d\d,|\b\d\d\d\d-\d\d\d\d,)*\Z""")


Hi,

try it without \A and \Z

import re
rx1=re.compile(r"""(\b\d\d\d\d,|\b\d\d\d\d-\d\d\d\d,)""")
print rx1.findall("1234,2222-8888,4567,")
# --> ['1234,', '2222-8888,', '4567,']

Thomas

--
Thomas Güttler, http://www.thomas-guettler.de/
Jul 26 '05 #2
Odd-R. wrote:
Input is a string of four digit sequences, possibly
separated by a -, for instance like this

"1234,2222-8888,4567,"

My regular expression is like this:

rx1=re.compile(r"""\A(\b\d\d\d\d,|\b\d\d\d\d-\d\d\d\d,)*\Z""")

When running rx1.findall("1234,2222-8888,4567,")

I only get the last match as the result. Isn't
findall suppose to return all the matches?


For a start, an expression that starts with \A and ends with \Z will
match the whole string (or not match at all). You have only one match.

Secondly, as you have a group in your expression, findall returns what
the group matches. Your expression matches zero or more of what your
group matches, provided there is nothing else at the start/end of the
string. The "zero or more" makes the re engine waltz about a bit; when
the music stopped, the group was matching "4567,".

Thirdly, findall should be thought of as merely a wrapper around a loop
using the search method -- it finds all non-overlapping matches of a
pattern. So the clue to get from this is that you need a really simple
pattern, like the following. You *don't* have to write an expression
that does the looping.

So here's the mean lean no-flab version -- you don't even need the
parentheses (sorry, Thomas).
rx1=re.compile(r"""\b\d\d\d\d,|\b\d\d\d\d-\d\d\d\d,""")
rx1.findall("1234,2222-8888,4567,")

['1234,', '2222-8888,', '4567,']

HTH,
John
Jul 26 '05 #3
John Machin wrote:
So here's the mean lean no-flab version -- you don't even need the
parentheses (sorry, Thomas).
rx1=re.compile(r"""\b\d\d\d\d,|\b\d\d\d\d-\d\d\d\d,""")
rx1.findall("1234,2222-8888,4567,") ['1234,', '2222-8888,', '4567,']


No flab? What about all that repetition of \d? A less flabby version:
rx1=re.compile(r"""\b\d{4}(?:-\d{4})?,""")
rx1.findall("1234,2222-8888,4567,")

['1234,', '2222-8888,', '4567,']

Jul 26 '05 #4
Duncan Booth wrote:
John Machin wrote:

So here's the mean lean no-flab version -- you don't even need the
parentheses (sorry, Thomas).

>rx1=re.compile(r"""\b\d\d\d\d,|\b\d\d\d\d-\d\d\d\d,""")
>rx1.findall("1234,2222-8888,4567,")


['1234,', '2222-8888,', '4567,']

No flab? What about all that repetition of \d? A less flabby version:

rx1=re.compile(r"""\b\d{4}(?:-\d{4})?,""")
rx1.findall("1234,2222-8888,4567,")


['1234,', '2222-8888,', '4567,']

OK, good idea to factor out the prefix and follow it by optional -1234.
However optimising re engines do common prefix factoring, *and* they
rewrite stuff like x{4} as xxxx.

Cheers,
John
Jul 26 '05 #5
On 2005-07-26, Duncan Booth <du**********@invalid.invalid> wrote:
rx1=re.compile(r"""\b\d{4}(?:-\d{4})?,""")
rx1.findall("1234,2222-8888,4567,")

['1234,', '2222-8888,', '4567,']


Thanks all for good advice. However this last expression
also matches the first four digits when the input is more
than four digits. To resolve this problem, I first do a
match of this,

regex=re.compile(r"""\A(\b\d{4},|\d{4}-\d{4},)*(\b\d{4}|\d{4}-\d{4})\Z""")

If this turns out ok, I do a find all with your expression, and then I get
the desired result.
--
Har du et kjøleskap, har du en TV
så har du alt du trenger for å leve

-Jokke & Valentinerne
Jul 27 '05 #6

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

Similar topics

3
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
7
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
6
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
3
by: jg | last post by:
I made a mistake somewhere in my vb code and I look, check and read against the articles and help on regex, I still can't find the mistake I made. I know my test string and the test patterns...
6
by: Talin | last post by:
I've run in to this problem a couple of times. Say I have a piece of text that I want to test against a large number of regular expressions, where a different action is taken based on which regex...
16
by: Mark Chambers | last post by:
Hi there, I'm seeking opinions on the use of regular expression searching. Is there general consensus on whether it's now a best practice to rely on this rather than rolling your own (string)...
7
by: =?Utf-8?B?amFj?= | last post by:
Hi, I have problems with following code and don’t find the bug : // Set ArrayList aArray = new ArrayList(); regStr = new Regex(@"\?)*(\d+)\]"); if(text != null && regStr.IsMatch(text))...
1
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose...
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...
0
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,...
0
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,...
0
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.