Searching backward

E

Evan

Hi guys,

This is a scaled down version of my work

I have a long string

$PASSENGERS ="Tom Departure Newark Arrives Sanfrasico FLIGHT_STATUS2
MORNING FLIGHT DEPARTS FROM DANE REACHES NEWARK AFTERNOON LEAVES
NEWARK AND REACHES SAN FRANSISCO NEXT MORNING"

I have multiple patterns to search within each string defined in a
separate file.

Departure|Arrival
MORNING|AFTERNOON

Need to search for the occurence and position of pattern 1, if found,
search for the occurence and position of pattern 2 and so on. The
search for reasons needs to be done in a backward direction.

Looked at rindex. It searches the string right to left but the problem
using it is that the string cannot be a regexp.

So the only option remains is to use a regexp.

Could anyone suggest any ideas in the direction of approaching this
problem.

- Evan
 
J

James Tolley

Evan said:
Hi guys,

I have multiple patterns to search within each string defined in a
separate file.

Departure|Arrival
MORNING|AFTERNOON

Need to search for the occurence and position of pattern 1, if found,
search for the occurence and position of pattern 2 and so on. The
search for reasons needs to be done in a backward direction.

Something like this should work, if I understand the issue. It's not tested,
though.

my $string = 'test this';
my @regexes = get_regexes(); # ('One|Two','Three|Four')

while(@regexes) {
my $regex = join '|', map {"($_)"} @regexes; # '(One|Two)|(Three|Four)'
$string =~ /.+($regex)/ or last; # get the regex that matches last in
the string
my $match = $1;
# do something with $match
}

hth
 
T

Tad McClellan

Evan said:
I have multiple patterns to search within each string defined in a
separate file.

Departure|Arrival
MORNING|AFTERNOON

Need to search for the occurence and position of pattern 1, if found,
search for the occurence and position of pattern 2 and so on. The
search for reasons needs to be done in a backward direction.

Looked at rindex. It searches the string right to left but the problem
using it is that the string cannot be a regexp.


So far you haven't said anything that *requires* a regex.

So the only option remains is to use a regexp.
^^^^^^^^^^^^^^^

Then there is something you are not telling us.

Could anyone suggest any ideas in the direction of approaching this
problem.


Call rindex() 4 times instead of doing 2 pattern matches with 2 alternatives.
 

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
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top