Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Wierd Regex stuff..

by Speedfreak (Sexton)
on May 21, 2000 at 16:25 UTC ( [id://14018]=perlquestion: print w/replies, xml ) Need Help??

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

Hej igen alla,

O.K, this time I've got a wierd regex problem. I thought I was doing this right but I guess not...

I'm parsing through a text file, line by line looking for a match on names. The actual list is airport names so I enter in "rlanda" as the string to match (partial name).

My logic is as follows:

      if ($lines1 =~ /$search*/i) {
            print $lines[0]." - ".$lines1."\n";
      }

It appears to work, matching regardless of case. However I'm getting very close but not exact matches. Heres a list of what (according to my logic) matches.

Summerland Automatic Weather Reporting System
Westerland / Sylt
Orland Iii
Stockholm / Arlanda
Latrobe / Westmorland
Orlando, Orlando International Airport
Orlando, Orlando Executive Airport
Orlando / Sanford Airport
Worland, Worland Municipal Airport
Uberlandia

It matching those with "rland" but why is it not using the last "a" in "rlanda" to filter it to just "Arlanda". Is it something to do with *?

Can anyone point out what the hell I'm doing wrong?

- Jed

Replies are listed 'Best First'.
RE: Wierd Regex stuff..
by cciulla (Friar) on May 21, 2000 at 17:08 UTC
    Yep. The * is saying, "ok, match the previous expression, 'a', zero or more times.

    What you may consider doing is somelthing like this:

    /$search(.)*/

    This way, it will match anything within $search, plus any character zero or more times.

      ... or just leave the "*" out. /dempa
Re: Wierd Regex stuff..
by Anonymous Monk on May 22, 2000 at 16:16 UTC
    Yeah I'd like to see more of the code. I think you are probably doing a chop of the variable that you read in and you are losing the "a" in your $search string... Or maybe not, more code would be better...
RE: Wierd Regex stuff..
by ZZamboni (Curate) on May 22, 2000 at 17:51 UTC
    First, make sure you use <code> instead of <pre>, so that your subscripts don't get turned into links :-)

    That said cciulla's answer is right on the money, so I'll just offer a slightly expanded version. When you use the expression

    /$search*/i
    Perl is replacing the value of the $search variable before matching it, so if $search contains "rlanda", the match expression becomes:
    /rlanda*/i
    Now, the asterisk (*) means "zero or more of the previous expression". Unless the "previous expression" is a parenthesized one, it means "previous character". So you are looking for anything that contains "rland" followed by zero or more a's. Hence all the spurious matches.

    From the looks of it, you don't need the asterisk at all. You will do fine with just

    /$search/i

    --ZZamboni

Re: Wierd Regex stuff..
by justinNEE (Monk) on May 21, 2000 at 22:14 UTC
    we don't believe you. paste more of the code, also, be sure you aren't just summarizing what you are doing...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-26 03:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found