Re: Match First Sequence in Regular Expression?

2006-01-28 Thread Armin Steinhoff
Alex Martelli wrote: Christoph Conrad [EMAIL PROTECTED] wrote: Hello Roger, since the length of the first sequence of the letter 'a' is 2. Yours accepts it, right? Yes, i misunderstood your requirements. So it must be modified essentially to that what Tim Chase wrote: m =

Re: Match First Sequence in Regular Expression?

2006-01-27 Thread Scott David Daniels
How about: pattern = re.compile('^([^a]|(a+[^ab]))*aaab') Which basically says, precede with arbitrarily many non-a's or a sequences ending in non-b, then must have 3 as followed by a b. cases = [xyz123aaabbab, xayz123aaabab, xaaayz123aaabab, xyz123babaaabab, xyz123aabbaaab,

Match First Sequence in Regular Expression?

2006-01-26 Thread Roger L. Cauvin
Say I have some string that begins with an arbitrary sequence of characters and then alternates repeating the letters 'a' and 'b' any number of times, e.g. xyz123aaabbaaabaaaabb I'm looking for a regular expression that matches the first, and only the first, sequence of the letter

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Christoph Conrad
Hello Roger, I'm looking for a regular expression that matches the first, and only the first, sequence of the letter 'a', and only if the length of the sequence is exactly 3. import sys, re, os if __name__=='__main__': m = re.search('a{3}', 'xyz123aaabbaaaabaaabb') print

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Sybren Stuvel
Roger L. Cauvin enlightened us with: I'm looking for a regular expression that matches the first, and only the first, sequence of the letter 'a', and only if the length of the sequence is exactly 3. Your request is ambiguous: 1) You're looking for the first, and only the first, sequence of

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Tim Chase
Say I have some string that begins with an arbitrary sequence of characters and then alternates repeating the letters 'a' and 'b' any number of times, e.g. xyz123aaabbaaabaaaabb I'm looking for a regular expression that matches the first, and only the first, sequence of the

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Alex Martelli
Tim Chase [EMAIL PROTECTED] wrote: ... I'm not quite sure what your intent here is, as the resulting find would obviously be aaa, of length 3. But that would also match ''; I think he wants negative loobehind and lookahead assertions around the 'aaa' part. But then there's the spec

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Roger L. Cauvin
Christoph Conrad [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Hello Roger, I'm looking for a regular expression that matches the first, and only the first, sequence of the letter 'a', and only if the length of the sequence is exactly 3. import sys, re, os if

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Roger L. Cauvin
Alex Martelli [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Tim Chase [EMAIL PROTECTED] wrote: ... I'm not quite sure what your intent here is, as the resulting find would obviously be aaa, of length 3. But that would also match ''; I think he wants negative loobehind and

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Christos Georgiou
On Thu, 26 Jan 2006 14:09:54 GMT, rumours say that Roger L. Cauvin [EMAIL PROTECTED] might have written: Say I have some string that begins with an arbitrary sequence of characters and then alternates repeating the letters 'a' and 'b' any number of times, e.g. xyz123aaabbaaabaaaabb

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Christoph Conrad
Hello Roger, since the length of the first sequence of the letter 'a' is 2. Yours accepts it, right? Yes, i misunderstood your requirements. So it must be modified essentially to that what Tim Chase wrote: m = re.search('^[^a]*a{3}b', 'xyz123aabbaaab') Best wishes from germany,

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Roger L. Cauvin
Sybren Stuvel [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Roger L. Cauvin enlightened us with: I'm looking for a regular expression that matches the first, and only the first, sequence of the letter 'a', and only if the length of the sequence is exactly 3. Your request is

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Tim Chase
Sorry for the confusion. The correct pattern should reject all strings except those in which the first sequence of the letter 'a' that is followed by the letter 'b' has a length of exactly three. Ah...a little more clear. r = re.compile([^a]*a{3}b+(a+b*)*) matches = [s for s

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Alex Martelli
Tim Chase [EMAIL PROTECTED] wrote: Sorry for the confusion. The correct pattern should reject all strings except those in which the first sequence of the letter 'a' that is followed by the letter 'b' has a length of exactly three. Ah...a little more clear. r =

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Christoph Conrad
Hallo Alex, r = re.compile([^a]*a{3}b+(a+b*)*) matches = [s for s in listOfStringsToTest if r.match(s)] Unfortunately, the OP's spec is even more complex than this, if we are to take to the letter what you just quoted; e.g. aazaaab SHOULD match, Then it's again a{3}b, isn't it? Freundliche

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Roger L. Cauvin
Tim Chase [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Sorry for the confusion. The correct pattern should reject all strings except those in which the first sequence of the letter 'a' that is followed by the letter 'b' has a length of exactly three. Ah...a little more clear.

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Roger L. Cauvin
Christos Georgiou [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] On Thu, 26 Jan 2006 14:09:54 GMT, rumours say that Roger L. Cauvin [EMAIL PROTECTED] might have written: Say I have some string that begins with an arbitrary sequence of characters and then alternates repeating the

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Tim Chase
r = re.compile([^a]*a{3}b+(a+b*)*) matches = [s for s in listOfStringsToTest if r.match(s)] Wow, I like it, but it allows some strings it shouldn't. For example: xyz123aabbaaab (It skips over the two-letter sequence of 'a' and matches 'bbaaab'.) Anchoring it to the beginning/end might

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Peter Hansen
Roger L. Cauvin wrote: Sorry for the confusion. The correct pattern should reject all strings except those in which the first sequence of the letter 'a' that is followed by the letter 'b' has a length of exactly three. Hope that's clearer . . . . Examples are a *really* good way to

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Roger L. Cauvin
Tim Chase [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] r = re.compile([^a]*a{3}b+(a+b*)*) matches = [s for s in listOfStringsToTest if r.match(s)] Wow, I like it, but it allows some strings it shouldn't. For example: xyz123aabbaaab (It skips over the two-letter sequence of 'a'

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Roger L. Cauvin
Peter Hansen [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Roger L. Cauvin wrote: Sorry for the confusion. The correct pattern should reject all strings except those in which the first sequence of the letter 'a' that is followed by the letter 'b' has a length of exactly three.

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Alex Martelli
Christoph Conrad [EMAIL PROTECTED] wrote: Hello Roger, since the length of the first sequence of the letter 'a' is 2. Yours accepts it, right? Yes, i misunderstood your requirements. So it must be modified essentially to that what Tim Chase wrote: m = re.search('^[^a]*a{3}b',

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Christos Georgiou
On Thu, 26 Jan 2006 16:26:57 GMT, rumours say that Roger L. Cauvin [EMAIL PROTECTED] might have written: Christos Georgiou [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] On Thu, 26 Jan 2006 14:09:54 GMT, rumours say that Roger L. Cauvin [EMAIL PROTECTED] might have written: Say I

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Roger L. Cauvin
Alex Martelli [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Tim Chase [EMAIL PROTECTED] wrote: Sorry for the confusion. The correct pattern should reject all strings except those in which the first sequence of the letter 'a' that is followed by the letter 'b' has a length of

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Christos Georgiou
On Thu, 26 Jan 2006 16:41:08 GMT, rumours say that Roger L. Cauvin [EMAIL PROTECTED] might have written: Good suggestion. Here are some test cases: xyz123aaabbab accept xyz123aabbaab reject xayz123aaabab accept xaaayz123abab reject xaaayz123aaabab accept Applying my last regex to your test

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Fredrik Lundh
Roger L. Cauvin wrote: Good suggestion. Here are some test cases: xyz123aaabbab accept xyz123aabbaab reject xayz123aaabab accept xaaayz123abab reject xaaayz123aaabab accept $ more test.py import re print gotexpected print -- testsuite = ( (xyz123aaabbab, accept),

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Dave Hansen
On Thu, 26 Jan 2006 16:26:57 GMT in comp.lang.python, Roger L. Cauvin [EMAIL PROTECTED] wrote: Christos Georgiou [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] [...] Is this what you mean? ^[^a]*(a{3})(?:[^a].*)?$ Close, but the pattern should allow arbitrary sequence of

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Roger L. Cauvin
Fredrik Lundh [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Roger L. Cauvin wrote: Good suggestion. Here are some test cases: xyz123aaabbab accept xyz123aabbaab reject xayz123aaabab accept xaaayz123abab reject xaaayz123aaabab accept $ more test.py import re print got

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Christos Georgiou
On Thu, 26 Jan 2006 18:01:07 +0100, rumours say that Fredrik Lundh [EMAIL PROTECTED] might have written: Roger L. Cauvin wrote: Good suggestion. Here are some test cases: xyz123aaabbab accept xyz123aabbaab reject xayz123aaabab accept xaaayz123abab reject xaaayz123aaabab accept $ more

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Roger L. Cauvin
Christos Georgiou [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] On Thu, 26 Jan 2006 16:41:08 GMT, rumours say that Roger L. Cauvin [EMAIL PROTECTED] might have written: Good suggestion. Here are some test cases: xyz123aaabbab accept xyz123aabbaab reject xayz123aaabab accept

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Roger L. Cauvin
Christos Georgiou [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] On Thu, 26 Jan 2006 16:26:57 GMT, rumours say that Roger L. Cauvin [EMAIL PROTECTED] might have written: Christos Georgiou [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] On Thu, 26 Jan 2006 14:09:54 GMT,

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Christos Georgiou
On Thu, 26 Jan 2006 17:09:18 GMT, rumours say that Roger L. Cauvin [EMAIL PROTECTED] might have written: Thanks, but the second test case I listed contained a typo. It should have contained a sequence of three of the letter 'a'. The test cases should be: xyz123aaabbab accept xyz123aabbaaab

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Roger L. Cauvin
Christos Georgiou [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] On Thu, 26 Jan 2006 18:01:07 +0100, rumours say that Fredrik Lundh [EMAIL PROTECTED] might have written: Roger L. Cauvin wrote: Good suggestion. Here are some test cases: xyz123aaabbab accept xyz123aabbaab reject

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Fredrik Lundh
Roger L. Cauvin wrote: $ python test.py gotexpected --- accept accept reject reject accept accept reject reject accept accept Thanks, but the second test case I listed contained a typo. It should have contained a sequence of three of the letter 'a'. The test

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Roger L. Cauvin
Christos Georgiou [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] On Thu, 26 Jan 2006 17:09:18 GMT, rumours say that Roger L. Cauvin [EMAIL PROTECTED] might have written: Thanks, but the second test case I listed contained a typo. It should have contained a sequence of three of the

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Roger L. Cauvin
Fredrik Lundh [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Roger L. Cauvin wrote: $ python test.py gotexpected --- accept accept reject reject accept accept reject reject accept accept Thanks, but the second test case I listed contained a typo. It

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Tim Chase
The below seems to pass all the tests you threw at it (taking the modified 2nd test into consideration) One other test that occurs to me would be xyz123aaabbaaabab where you have aaab in there twice. -tkc import re tests = [ (xyz123aaabbab,True), (xyz123aabbaaab, False),

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Michael Spencer
Roger L. Cauvin wrote: Fredrik Lundh [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Roger L. Cauvin wrote: $ python test.py gotexpected --- accept accept reject reject accept accept reject reject accept accept Thanks, but the second test case I listed

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Roger L. Cauvin
Michael Spencer [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Roger L. Cauvin wrote: xyz123aaabbab accept xyz123aabbaaab reject xayz123aaabab accept xaaayz123abab reject xaaayz123aaabab accept This passes your tests. I haven't closely followed the thread for other

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Roger L. Cauvin
Tim Chase [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] The below seems to pass all the tests you threw at it (taking the modified 2nd test into consideration) One other test that occurs to me would be xyz123aaabbaaabab where you have aaab in there twice. Good suggestion.

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Tim Chase
xyz123aaabbaaabab where you have aaab in there twice. Good suggestion. I assumed that this would be a valid case. If not, the expression would need tweaking. ^([^b]|((?!a)b))*aaab+[ab]*$ Looks good, although I've been unable to find a good explanation of the negative lookbehind

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Peter Hansen
Roger L. Cauvin wrote: Michael Spencer [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Roger L. Cauvin wrote: xyz123aaabbab accept xyz123aabbaaab reject xayz123aaabab accept xaaayz123abab reject xaaayz123aaabab accept This passes your tests. I haven't closely followed the thread

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Roger L. Cauvin
Peter Hansen [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Roger L. Cauvin wrote: Michael Spencer [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Roger L. Cauvin wrote: xyz123aaabbab accept xyz123aabbaaab reject xayz123aaabab accept xaaayz123abab reject xaaayz123aaabab

Re: Match First Sequence in Regular Expression?

2006-01-26 Thread Alex Martelli
Christoph Conrad [EMAIL PROTECTED] wrote: Hallo Alex, r = re.compile([^a]*a{3}b+(a+b*)*) matches = [s for s in listOfStringsToTest if r.match(s)] Unfortunately, the OP's spec is even more complex than this, if we are to take to the letter what you just quoted; e.g. aazaaab SHOULD