Searching backward

Discussion in 'Perl Misc' started by Evan, Nov 21, 2004.

  1. Evan

    Evan Guest

    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
     
    Evan, Nov 21, 2004
    #1
    1. Advertising

  2. Evan

    James Tolley Guest

    "Evan" <> wrote in message
    news:...
    > 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
     
    James Tolley, Nov 21, 2004
    #2
    1. Advertising

  3. Evan <> wrote:

    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 22, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. paul
    Replies:
    6
    Views:
    383
  2. =?Utf-8?B?TUxpYmJ5?=

    Netscape - Backward compatibility testing

    =?Utf-8?B?TUxpYmJ5?=, Sep 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    646
    [MSFT]
    Sep 6, 2004
  3. Mick

    Simulate the IE backward button

    Mick, Dec 15, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    401
  4. Steve C. Orr [MVP, MCSD]

    Re: disable backward navigation of the browser

    Steve C. Orr [MVP, MCSD], Mar 7, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    2,481
    Jeffrey Palermo
    Mar 7, 2005
  5. stumblng.tumblr
    Replies:
    1
    Views:
    234
    stumblng.tumblr
    Feb 4, 2008
Loading...

Share This Page