Pls help with a Regex

  • Thread starter David Joseph Bonnici
  • Start date
D

David Joseph Bonnici

I have this sort of information in a text file

19:00 STEPPING THE WORLD: Greek 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
Greek 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
 
G

Gunnar Hjalmarsson

David said:
I have this sort of information in a text file

19:00 STEPPING THE WORLD: Greek 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
Greek 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(.*))?$
 
T

Tad McClellan

David Joseph Bonnici said:
I have this sort of information in a text file

19:00 STEPPING THE WORLD: Greek 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
Greek 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().
 
D

David Joseph Bonnici

Thanks it works fine.

Gunnar Hjalmarsson said:
David said:
I have this sort of information in a text file

19:00 STEPPING THE WORLD: Greek 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
Greek 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(.*))?$
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top