Re: a RegEx puzzle (end of thread)

2005-03-12 Thread Charles Hartman
Won't extend this except to say thanks to Michael Spencer for another version. If I were doing it only once I'd use that. Since I do it more than once I should package it as a function. Thanks. Charles Hartman Professor of English, Poet in Residence http://cherry.conncoll.edu/cohar http://villex

Re: a RegEx puzzle

2005-03-11 Thread Michael Spencer
Charles Hartman wrote: I'm still shaky on some of sre's syntax. Here's the task: I've got strings (never longer than about a dozen characters) that are guaranteed to be made only of characters 'x' and '/'. In each string I want to find the longest continuous stretch of pairs whose first characte

Re: a RegEx puzzle

2005-03-11 Thread Peter Otten
Charles Hartman wrote: > If I'm understand you right, then I still didn't explain clearly. I think you explained it clear enough. Mine was a confused post which would never have seen the light of c.l.py had I checked it with an example before hitting . Sorry. Peter -- http://mail.python.org/m

Re: a RegEx puzzle

2005-03-11 Thread Charles Hartman
Thanks -- not only for the code, which does almost exactly what I need to do, but for the reminder (thanks also to Jeremy Bowers for this!) to prefer simple solutions. I was, of course, so tied up in getting my nifty one-liner right that I totally lost sight of how straightforwardly the job cou

Re: a RegEx puzzle

2005-03-11 Thread Charles Hartman
If I'm understand you right, then I still didn't explain clearly. (Surprise!) If the string is '//xx//' then possible matches are at position 6 and 7 (both length 2, so "longest" doesn't even come into it). My code searches from position 0, then 1, then 2, and so on, to catch every possible

Re: a RegEx puzzle

2005-03-11 Thread Kent Johnson
Charles Hartman wrote: I'm still shaky on some of sre's syntax. Here's the task: I've got strings (never longer than about a dozen characters) that are guaranteed to be made only of characters 'x' and '/'. In each string I want to find the longest continuous stretch of pairs whose first characte

Re: a RegEx puzzle

2005-03-11 Thread Jeremy Bowers
On Fri, 11 Mar 2005 08:38:36 -0500, Charles Hartman wrote: > I'm still shaky on some of sre's syntax. Here's the task: I've got > strings (never longer than about a dozen characters) that are > guaranteed to be made only of characters 'x' and '/'. In each string I > want to find the longest con

Re: a RegEx puzzle

2005-03-11 Thread Peter Otten
Charles Hartman wrote: > pat = sre.compile('(x[x/])+') > (longest, startlongest) = max([(fnd.end()-fnd.start(), fnd.start()) for > i in range(len(marks)) > for fnd in pat.finditer(marks,i)]) If I'm understanding that correctly, the only way for you to get different best matches are at offsets 0 a

Re: a RegEx puzzle

2005-03-11 Thread Roy Smith
Charles Hartman <[EMAIL PROTECTED]> wrote: >> Hmmm. Are you, by any chance, looking for meter patterns in verse? > >Yes, of course. Ah, now it's a lot clearer. When you said you were looking for "pairs", I was thinking "pairs of numbers, representing the start and end of the string". Now that

Re: a RegEx puzzle

2005-03-11 Thread Charles Hartman
Charles Hartman <[EMAIL PROTECTED]> wrote: I'm still shaky on some of sre's syntax. Here's the task: I've got strings (never longer than about a dozen characters) that are guaranteed to be made only of characters 'x' and '/'. One possibility is to cheat completely, and depending on memory constra

Re: a RegEx puzzle

2005-03-11 Thread Roy Smith
Charles Hartman <[EMAIL PROTECTED]> wrote: > I'm still shaky on some of sre's syntax. Here's the task: I've got > strings (never longer than about a dozen characters) that are > guaranteed to be made only of characters 'x' and '/'. One possibility is to cheat completely, and depending on memory

a RegEx puzzle

2005-03-11 Thread Charles Hartman
I'm still shaky on some of sre's syntax. Here's the task: I've got strings (never longer than about a dozen characters) that are guaranteed to be made only of characters 'x' and '/'. In each string I want to find the longest continuous stretch of pairs whose first character is 'x' and the secon