Home > Archive > PERL Miscellaneous > April 2005 > Pls help with a Regex
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Pls help with a Regex
|
|
| David Joseph Bonnici 2005-04-26, 3:59 am |
| I have this sort of information in a text file
19:00 STEPPING THE WORLD: Gr Islands - Corfu.
and I am trying to build a regex that will put
19 into backreference 1
00 into backreference 2
STEPPING THE WORLD into backreference 3
Gr Islands - Corfu into backreference 4
I have built this ^(\d{2})(?::|\.)(\d{2})\s(.*)(?::\s)(.*).$ and it works fine
but I want that if I have something like this
19:00 STEPPING THE WORLD
I would still have backreference 1,2, and 3 filled.
Any help
| |
| Gunnar Hjalmarsson 2005-04-26, 3:59 am |
| David Joseph Bonnici wrote:
> I have this sort of information in a text file
>
> 19:00 STEPPING THE WORLD: Gr Islands - Corfu.
>
> and I am trying to build a regex that will put
>
> 19 into backreference 1
> 00 into backreference 2
> STEPPING THE WORLD into backreference 3
> Gr Islands - Corfu into backreference 4
>
> I have built this ^(\d{2})(?::|\.)(\d{2})\s(.*)(?::\s)(.*).$ and it works fine
>
> but I want that if I have something like this
> 19:00 STEPPING THE WORLD
>
> I would still have backreference 1,2, and 3 filled.
Then make the last part optional:
^(\d{2})(?::|\.)(\d{2})\s(.*?)(?:(?::\s)(.*).)?$
--------------------------------^-^^^-------------^
or slightly simplified:
^(\d{2})[:.](\d{2})\s(.*?)(?::\s(.*))?$
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| Tad McClellan 2005-04-26, 3:59 am |
| David Joseph Bonnici <djb@global.net.mt> wrote:
> I have this sort of information in a text file
>
> 19:00 STEPPING THE WORLD: Gr Islands - Corfu.
^^^
^^^
That was a tab character in your post, I plan to make use of that.
> and I am trying to build a regex that will put
>
> 19 into backreference 1
> 00 into backreference 2
> STEPPING THE WORLD into backreference 3
> Gr Islands - Corfu into backreference 4
>
> I have built this ^(\d{2})(?::|\.)(\d{2})\s(.*)(?::\s)(.*).$ and it works fine
It looks like "saying what you want to keep"[1] is hard...
> but I want that if I have something like this
> 19:00 STEPPING THE WORLD
>
> I would still have backreference 1,2, and 3 filled.
"saying what you want to throw away"[2] may be easier.
If the string is already in $_, then:
my @fields = split /:\s*|\t/;
Figuring out how to get rid of the dot from the end of the last
field is left as an exercise for the reader.
Randal has a saying:
[1] If you want to say what to keep then use m// in list context.
[2] If you want to say what to throw away then use split().
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
| |
| David Joseph Bonnici 2005-04-26, 8:58 am |
| Thanks it works fine.
"Gunnar Hjalmarsson" <noreply@gunnar.cc> wrote in message
news:3d5e2rF6pqfjvU1@individual.net...
> David Joseph Bonnici wrote:
>
> Then make the last part optional:
>
> ^(\d{2})(?::|\.)(\d{2})\s(.*?)(?:(?::\s)(.*).)?$
> --------------------------------^-^^^-------------^
>
> or slightly simplified:
>
> ^(\d{2})[:.](\d{2})\s(.*?)(?::\s(.*))?$
>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl
|
|
|
|
|