Re: [Tutor] regular expression question

2009-04-28 Thread Kent Johnson
On Tue, Apr 28, 2009 at 4:03 AM, Kelie wrote: > Hello, > > The following code returns 'abc123abc45abc789jk'. How do I revise the pattern > so > that the return value will be 'abc789jk'? In other words, I want to find the > pattern 'abc' that is closest to 'jk'. Here the string '123', '45' and '78

Re: [Tutor] regular expression question

2009-04-28 Thread Kent Johnson
2009/4/28 Marek spociń...@go2.pl,Poland : >> import re >> s = 'abc123abc45abc789jk' >> p = r'abc.+jk' >> lst = re.findall(p, s) >> print lst[0] > > I suggest using r'abc.+?jk' instead. > > the additional ? makes the preceeding '.+' non-greedy so instead of matching > as long string as it can it m

Re: [Tutor] regular expression question

2009-04-28 Thread Kelie
spir free.fr> writes: > To avoid that, use non-grouping parens (?:...). This also avoids the need for parens around the whole format: > p = Pattern(r'abc(?:(?!abc).)+jk') > print p.findall(s) > ['abc789jk'] > > Denis This one works! Thank you Denis. I'll try it out on the actual much longer (m

Re: [Tutor] regular expression question

2009-04-28 Thread Kelie
Andre Engels gmail.com> writes: > > 2009/4/28 Marek Spociński go2.pl,Poland 10g.pl>: > > I suggest using r'abc.+?jk' instead. > > > > That was my first idea too, but it does not work for this case, > because Python will still try to _start_ the match as soon as > possible. yeah, i tried t

Re: [Tutor] regular expression question

2009-04-28 Thread spir
Le Tue, 28 Apr 2009 11:06:16 +0200, Marek spociń...@go2.pl, Poland s'exprima ainsi: > > Hello, > > > > The following code returns 'abc123abc45abc789jk'. How do I revise the > > pattern so that the return value will be 'abc789jk'? In other words, I > > want to find the pattern 'abc' that is clos

Re: [Tutor] regular expression question

2009-04-28 Thread Marek Spociński , Poland
Dnia 28 kwietnia 2009 11:16 Andre Engels napisał(a): > 2009/4/28 Marek spociń...@go2.pl,Poland : > >> Hello, > >> > >> The following code returns 'abc123abc45abc789jk'. How do I revise the > >> pattern so > >> that the return value will be 'abc789jk'? In other words, I want to find > >> the > >>

Re: [Tutor] regular expression question

2009-04-28 Thread Andre Engels
2009/4/28 Marek spociń...@go2.pl,Poland : >> Hello, >> >> The following code returns 'abc123abc45abc789jk'. How do I revise the >> pattern so >> that the return value will be 'abc789jk'? In other words, I want to find the >> pattern 'abc' that is closest to 'jk'. Here the string '123', '45' and '7

Re: [Tutor] regular expression question

2009-04-28 Thread =?UTF-8?Q?Marek_Spoci=C5=84ski
> Hello, > > The following code returns 'abc123abc45abc789jk'. How do I revise the pattern > so > that the return value will be 'abc789jk'? In other words, I want to find the > pattern 'abc' that is closest to 'jk'. Here the string '123', '45' and '789' > are > just examples. They are actually q

Re: [Tutor] regular expression question

2005-04-07 Thread Danny Yoo
> I wonder if anyone can help me with an RE. I also wonder if there is an > RE mailing list anywhere - I haven't managed to find one. Hi Debbie, I haven't found one either. There appear to be a lot of good resources here: http://dmoz.org/Computers/Programming/Languages/Regular_Expressions

Re: [Tutor] regular expression question

2005-04-07 Thread Kent Johnson
D Elliott wrote: I wonder if anyone can help me with an RE. I also wonder if there is an RE mailing list anywhere - I haven't managed to find one. I'm trying to use this regular expression to delete particular strings from a file before tokenising it. I want to delete all strings that have a fu

Re: [Tutor] regular expression question

2005-03-09 Thread Kent Johnson
Mike Hall wrote: A simple example will show what I mean: >>> import re >>> x = re.compile(r"(A) | (B)") >>> s = "X R A Y B E" >>> r = x.sub("13", s) >>> print r X R 13Y13 E ...so unless I'm understanding it wrong, "B" is supposed to be ignored if "A" is matched, yet I get both matched. I get

Re: [Tutor] regular expression question

2005-03-09 Thread Mike Hall
but yeah, it seems you're expecting it to examine the string as a whole. I guess I was, good point. On Mar 9, 2005, at 12:28 PM, Liam Clarke wrote: Actually, you should get that anyway... """ | Alternation, or the ``or'' operator. If A and B are regular expressions, A|B will match any string t

Re: [Tutor] regular expression question

2005-03-09 Thread Liam Clarke
Oops I mean for i in range(len(k)): i f k[i] == 'A' or k[i]=='B': k[i ]= 13 On Thu, 10 Mar 2005 09:28:59 +1300, Liam Clarke <[EMAIL PROTECTED]> wrote: > Actually, you should get that anyway... > > """ > | > Alternation, or the ``or'' operator. If A and B are regular > express

Re: [Tutor] regular expression question

2005-03-09 Thread Liam Clarke
Actually, you should get that anyway... """ | Alternation, or the ``or'' operator. If A and B are regular expressions, A|B will match any string that matches either "A" or "B". | has very low precedence in order to make it work reasonably when you're alternating multi-character strings. Crow|S

Re: [Tutor] regular expression question

2005-03-09 Thread Mike Hall
But I only want to ignore "B" if "A" is a match. If "A" is not a match, I'd like it to advance on to "B". On Mar 9, 2005, at 12:07 PM, Marcos Mendonça wrote: Hi Not and regexp expert. But it seems to me that if you want to ignora "B" then it should be (A) | (^B) Hope it helps! On Wed, 9 Mar 2005

Re: [Tutor] regular expression question

2005-03-09 Thread Mike Hall
Indeed I do: >>> import re >>> x = re.compile('A|B') >>> s = " Q A R B C" >>> r = x.sub("13", s) >>> print r Q 13 R 13 C On Mar 9, 2005, at 12:09 PM, Liam Clarke wrote: Hi Mike, Do you get the same results for a search pattern of 'A|B'? On Wed, 9 Mar 2005 11:11:57 -0800, Mike Hall <[EMAIL PROTECT

Re: [Tutor] regular expression question

2005-03-09 Thread Liam Clarke
Hi Mike, Do you get the same results for a search pattern of 'A|B'? On Wed, 9 Mar 2005 11:11:57 -0800, Mike Hall <[EMAIL PROTECTED]> wrote: > I'm having some strange results using the "or" operator. In every test > I do I'm matching both sides of the "|" metacharacter, not one or the > other a

Re: [Tutor] regular expression question

2005-03-09 Thread Mike Hall
I'm having some strange results using the "or" operator. In every test I do I'm matching both sides of the "|" metacharacter, not one or the other as all documentation says it should be (the parser supposedly scans left to right, using the first match it finds and ignoring the rest). It should

Re: [Tutor] regular expression question

2005-03-08 Thread Mike Hall
Sorry, my last reply crossed this one (and yes, I forgot again to CC the list). I'm experimenting now with your use of the "or" operator( "|") between two expressions, thanks. On Mar 8, 2005, at 6:42 PM, Danny Yoo wrote: On Tue, 8 Mar 2005, Mike Hall wrote: Yes, my existing regex is using a lo

Re: [Tutor] regular expression question

2005-03-08 Thread Danny Yoo
> > Regular expressions are a little evil at times; here's what I think you're > thinking of: > > ### > >>> import re > >>> pattern = re.compile(r"""dog(?!cat) > ...| (?<=dogcat)""", re.VERBOSE) > >>> pattern.match('dogman').start() > 0 > >>> pattern.search('dogcatcher').start

Re: [Tutor] regular expression question

2005-03-08 Thread Mike Hall
This will match the position in front of "dog": (?<=dog) This will match the position in front of "cat": (?<=cat) This will not match in front of "dog" if "dog" is followed by "cat": (?<=dog)\b (?!cat) Now my question is how to get this: (?<=cat) ...but ONLY if "cat" is following "dog." If "dog" do

Re: [Tutor] regular expression question

2005-03-08 Thread Danny Yoo
On Tue, 8 Mar 2005, Mike Hall wrote: > Yes, my existing regex is using a look behind assertion: > > (?<=dog) > > ...it's also checking the existence of "Cat": > > (?!Cat) > > ...what I'm stuck on is how to essentially use a lookbehind on "Cat", > but only if it exists. Hi Mike, [Note: Please

Re: [Tutor] regular expression question

2005-03-08 Thread Sean Perry
Mike Hall wrote: First, thanks for the response. Using your re: my_re = re.compile(r'(dog)(cat)?') ...I seem to simply be matching the pattern "Dog". Example: >>> str1 = "The dog chased the car" >>> str2 = "The dog cat parade was under way" >>> x1 = re.compile(r'(dog)(cat)?') >>> rep1 = x1.su

Re: [Tutor] regular expression question

2005-03-08 Thread Mike Hall
First, thanks for the response. Using your re: my_re = re.compile(r'(dog)(cat)?') ...I seem to simply be matching the pattern "Dog". Example: >>> str1 = "The dog chased the car" >>> str2 = "The dog cat parade was under way" >>> x1 = re.compile(r'(dog)(cat)?') >>> rep1 = x1.sub("REPLACE", str1) >>>

Re: [Tutor] regular expression question

2005-03-08 Thread Danny Yoo
On Tue, 8 Mar 2005, Mike Hall wrote: > I'd like to get a match for a position in a string preceded by a > specified word (let's call it "Dog"), unless that spot in the string > (after "Dog") is directly followed by a specific word(let's say "Cat"), > in which case I want my match to occur direct

Re: [Tutor] regular expression question

2005-03-08 Thread Sean Perry
Mike Hall wrote: I'd like to get a match for a position in a string preceded by a specified word (let's call it "Dog"), unless that spot in the string (after "Dog") is directly followed by a specific word(let's say "Cat"), in which case I want my match to occur directly after "Cat", and not "Dog