Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Pattern matching forwards/backwards???

by Anonymous Monk
on Jun 15, 2009 at 10:48 UTC ( [id://771612]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello all!
Say you have a string:
KLEWREGREG-----REGREGEWD....-EWGREGERABCSHRE..----...XFGHWW
(you can have only - and . between letters).
If I want to "expand" XFGHWW to get the 3 most close letters to it (HRE from EWGREGERABCSHRE), how could I write a pattern match?
My goal is to read the search backwards, that is I won't know beforehand the format of the total string... That means that the algorithm would be like:
1. Find substring XFGHWW
2. From its left, get 3 letters, that are now . or -
3. Print the new substring.

Replies are listed 'Best First'.
Re: Pattern matching forwards/backwards???
by JavaFan (Canon) on Jun 15, 2009 at 11:06 UTC
    What I would do:
    my $original = 'KLEWREGREG-----REGREGEWD....-EWGREGERABCSHRE..----...X +FGHWW'; my $copy = $original; $copy =~ s/[-.]+//g; if ($copy =~ /(...)XFGHWW/) { print "Found it: $1\n"; }
    Of course, if there's no need to preserve the original string, there's no need to make a copy.
Re: Pattern matching forwards/backwards???
by Crian (Curate) on Jun 15, 2009 at 11:00 UTC

    1. /\QXFGHWW\E/

    2. /([^-.]{3})/

    together:

    $_ = 'KLEWREGREG-----REGREGEWD....-EWGREGERABCSHRE..----...XFGHWW'; /([^-.]{3})[-.]*\QXFGHWW\E/; print $1;

      That assumes there are no dashes and dots between the three letters searched for. It would fail on:
      $_ = 'A-BCXFGHWW';
        A small change and it will work again:
        $_ = 'A-BCXFGHWW'; print join '', m/([^-.])[-.]*([^-.])[-.]*([^-.])[-.]*\QXFGHWW\E/;

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Pattern matching forwards/backwards???
by 13warrior (Acolyte) on Jun 15, 2009 at 12:02 UTC
    We can use split with a regex to get the second last string 1 and then apply another regex to get the last three characters in the second last string1
    #!/usr/bin/perl use strict; my $string = "KLEWREGREG-----REGREGEWD....-EWGREGERABCSHRT..----...XFG +HWW....----....TRYU"; my @array = split ("[.-]+",$string); my $interested = $array[$#array-1]; print $1 if ( $interested =~ m/([a-zA-Z]{3}$)/); Output : HWW

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://771612]
Approved by targetsmart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-28 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found