Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #11889
| From | Matt Funk <matze999@gmail.com> |
|---|---|
| Subject | Re: Help with regular expression in python |
| Date | 2011-08-19 15:55 -0600 |
| References | <201108181349.54727.matze999@gmail.com> <mailman.227.1313775252.27778.python-list@python.org> <c83c4dd3-7451-4c55-81d5-ae9c575381b1@glegroupsg2000goo.googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.246.1313790971.27778.python-list@python.org> (permalink) |
On Friday, August 19, 2011, Carl Banks wrote:
> On Friday, August 19, 2011 10:33:49 AM UTC-7, Matt Funk wrote:
> > number = r"\d\.\d+e\+\d+"
> > numbersequence = r"%s( %s){31}(.+)" % (number,number)
> > instance_linetype_pattern = re.compile(numbersequence)
> >
> > The results obtained are:
> > results:
> > [(' 2.199000e+01', ' : (instance: 0)\t:\tsome description')]
> > so this matches the last number plus the string at the end of the line,
> > but no retaining the previous numbers.
> >
> > Anyway, i think at this point i will go another route. Not sure where the
> > issues lies at this point.
>
> I think the problem is that repeat counts don't actually repeat the
> groupings; they just repeat the matchings. Take this expression:
>
> r"(\w+\s*){2}"
I see
>
> This will match exactly two words separated by whitespace. But the match
> result won't contain two groups; it'll only contain one group, and the
> value of that group will match only the very last thing repeated:
>
> Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
> [GCC 4.5.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>
> >>> import re
> >>> m = re.match(r"(\w+\s*){2}","abc def")
> >>> m.group(1)
>
> 'def'
>
> So you see, the regular expression is doing what you think it is, but the
> way it forms groups is not.
>
>
> Just a little advice (I know you've found a different method, and that's
> good, this is for the general reader).
>
> The functions re.findall and re.finditer could have helped here, they find
> all the matches in a string and let you iterate through them. (findall
> returns the strings matched, and finditer returns the sequence of match
> objects.) You could have done something like this:
I did use findall but when i tried to match the everything (including the 'some
description' part) it did not work. But i think the explanation you gave above
matches this case and explains why it did not.
>
> row = [ float(x) for x in re.findall(r'\d+\.\d+e\+d+',line) ]
>
> And regexp matching is often overkill for a particular problem; this may be
> of them. line.split() could have been sufficient:
>
> row = [ float(x) for x in line.split() ]
>
> Of course, these solutions don't account for the case where you have lines,
> some of which aren't 32 floating-point numbers. You need extra error
> handling for that, but you get the idea.
thanks
matt
>
>
> Carl Banks
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Help with regular expression in python Matt Funk <matze999@gmail.com> - 2011-08-19 09:20 -0600
Re: Help with regular expression in python jmfauth <wxjmfauth@gmail.com> - 2011-08-19 08:50 -0700
Re: Help with regular expression in python Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-08-19 18:00 +0200
Re: Help with regular expression in python Matt Funk <matze999@gmail.com> - 2011-08-19 11:33 -0600
Re: Help with regular expression in python jmfauth <wxjmfauth@gmail.com> - 2011-08-19 11:40 -0700
Re: Help with regular expression in python Matt Funk <matze999@gmail.com> - 2011-08-19 15:21 -0600
Re: Help with regular expression in python "rurpy@yahoo.com" <rurpy@yahoo.com> - 2011-08-19 12:55 -0700
Re: Help with regular expression in python MRAB <python@mrabarnett.plus.com> - 2011-08-19 21:43 +0100
Re: Help with regular expression in python Carl Banks <pavlovevidence@gmail.com> - 2011-08-19 13:11 -0700
Re: Help with regular expression in python Matt Funk <matze999@gmail.com> - 2011-08-19 15:55 -0600
Re: Help with regular expression in python Vlastimil Brom <vlastimil.brom@gmail.com> - 2011-08-22 15:59 +0200
csiph-web