[Gllug] matching strings in python
stuart at sjsears.com
Sun Oct 2 15:22:05 UTC 2011
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
> > 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', 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...
(this time being specific that the numbers are at the beginning of each
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:
Or you essentially re-run the re.compile process for every line.
> 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 :)
Stuart Sears RHCA etc.
"It's today!" said Piglet.
"My favourite day," said Pooh.
Gllug mailing list - Gllug at gllug.org.uk
More information about the GLLUG