Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Pls help with a Regex
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 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

Report this thread to moderator Post Follow-up to this message
Old Post
David Joseph Bonnici
04-26-05 08:59 AM


Re: Pls help with a Regex
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

Report this thread to moderator Post Follow-up to this message
Old Post
Gunnar Hjalmarsson
04-26-05 08:59 AM


Re: Pls help with a Regex
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[/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

Report this thread to moderator Post Follow-up to this message
Old Post
Tad McClellan
04-26-05 08:59 AM


Re: Pls help with a Regex
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



Report this thread to moderator Post Follow-up to this message
Old Post
David Joseph Bonnici
04-26-05 01:58 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Miscellaneous archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:30 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.