[PLUG] Long Perl regex...

Jeff Schwaber freyley at gmx.net
Thu Sep 18 13:07:02 UTC 2003


On Thu, 2003-09-18 at 12:58, Jeme A Brelin wrote:
> On Thu, 18 Sep 2003, Matt Alexander wrote:
> > I have 6 numbers that appear at the end of a line of text.  The 6 numbers
> > may or may not have spaces between each.  So, for example, I could have:
> >
> > 1 234 5 6
> > or
> > 1234 56

> Doesn't that work?

Or, in a readable language, and without using regexes:

#!/usr/bin/python
# get your string from somewhere, save it in variable line

i=0, end=-1, numbers=""
while i < 6:
    if line[end] != " ":
        numbers = numbers + line[end]
        i = i + 1
    end = end - 1

Of course, you never said anything about this either running incredibly
quickly (this is okay, but poky compared to a regex) or that spaces were
the only non-numeric characters in the midst of those numbers. Plus you
wanted to put it in a bash script.

But I'm just having fun. =)

Jeff





More information about the PLUG mailing list