Code Comments
Programming Forum and web based access to our favorite programming groups.I have this sort of information in a text file 19:00 STEPPING THE WORLD: GrIslands - 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 fi ne 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
Post Follow-up to this messageDavid Joseph Bonnici wrote: > I have this sort of information in a text file > > 19:00 STEPPING THE WORLD: GrIslands - 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
Post Follow-up to this messageDavid Joseph Bonnici <djb@global.net.mt> wrote: > I have this sort of information in a text file > > 19:00 STEPPING THE WORLD: GrIslands - 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[/co lor] 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
Post Follow-up to this messageThanks 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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.