[Gllug] matching strings in python
dudes dudes
akam123 at hotmail.co.uk
Sun Oct 2 20:46:11 UTC 2011
thanks very much Stuart for your explanation. Kind of you
> Date: Sun, 2 Oct 2011 16:22:05 +0100
> From: stuart at sjsears.com
> To: gllug at gllug.org.uk
> Subject: Re: [Gllug] matching strings in python
>
> On 02/10/2011 11:08, dudes dudes wrote:
> >
> >
> > >
> > > RTFM :)
> >
> > TFM has been Read b4 starting the project :P
>
> well, that's very good. Did you go back to it when your pattern wasn't
> working?
>
> > > http://docs.python.org/library/re.html#matching-vs-searching
> > >
> > > specifically:
> > > where are these numbers on each line? Are they at the start? if not,
> > > re.match won't find them without a preceding wildcard.
> >
> > yes there are one each line and at the start of each
>
> so your pattern can be simpler.
>
> > > Also, there's no need for the 'this=...' bit, unless you actually feel
> > > you need the variable.
> >
> > yes I need "this" as variable even though it has a different variable
> > name in the source code.
> >
> > > Something like the following
> > >
> > > for line in open('filename'):
> > > if re.search('1000[01]', line):
> > > print line
> >
> >
> > > is more likely to succeed
> >
> > YES True, but there are 10.000 different digit numbers, but I only need
> > to find 10 numbers .. such as 10000, 10001, 1286, 3412, 3512, 456,
> > 653,,,etc and they are different. Therefore I need to retrieve one out
> > of these 10 numbers !
>
> The regex pattern I gave you matches '10000' and '10001', which was all
> you asked for.
> Your response above sort-of suggests that you don't realise this?
>
> It would be fairly trivial to extend it to match all the numbers you
> want. Something like...
>
> re.match('^(1000[01]|653|1286|...)', line)
>
> (this time being specific that the numbers are at the beginning of each
> line.)
>
> another point: how many lines are there in the file?
> For more than a small number it is arguably more efficient to do this:
>
> numpatt = re.compile(PATTERN)
> for line in this:
> if numpatt.match(line):
> print line
>
> Or you essentially re-run the re.compile process for every line.
>
> [snip]
> > yes True but using DrPython as an editor won't let you to save the code
> > if you have syntax issues.
>
> We don't know that.
>
> > > This example was fairly trivial, but if you are asking for help with
> > > code like this, it would help if you actually post working excerpts from
> > > it. Or excerpts that throw the errors you are asking about.
> >
> > agree and thank you :)
>
> Regards,
>
> Stuart
> --
> Stuart Sears RHCA etc.
> "It's today!" said Piglet.
> "My favourite day," said Pooh.
> --
> Gllug mailing list - Gllug at gllug.org.uk
> http://lists.gllug.org.uk/mailman/listinfo/gllug
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.lug.org.uk/pipermail/gllug/attachments/20111002/aa008cd5/attachment.html>
-------------- next part --------------
--
Gllug mailing list - Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
More information about the GLLUG
mailing list