Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Match nothing in grep { ! // }

by Anonymous Monk
on Mar 14, 2013 at 06:38 UTC ( #1023384=note: print w/ replies, xml ) Need Help??


in reply to Match nothing in grep { ! // }

Yeah , and then what? What is your question?

The empty regex always matches

$ perl -Mre=debug -e " m//" Compiling REx "" Final program: 1: NOTHING (2) 2: END (0) minlen 0 Matching REx "" against "" 0 <> <> | 1:NOTHING(2) 0 <> <> | 2:END(0) Match successful! Freeing REx: "" $ perl -Mre=debug -e " $_ = q/fi/; m//" Compiling REx "" Final program: 1: NOTHING (2) 2: END (0) minlen 0 Matching REx "" against "fi" 0 <> <fi> | 1:NOTHING(2) 0 <> <fi> | 2:END(0) Match successful! Freeing REx: ""


Comment on Re: Match nothing in grep { ! // }
Download Code
Re^2: Match nothing in grep { ! // }
by ag4ve (Monk) on Mar 14, 2013 at 08:20 UTC
    I'm writing a scraper and sometimes I need to exclude some data. As I said, doing:

    $css->{xlink} //= ' ';
    works in this case but it just doesn't seem like the right way to do this.

    @{$ret->{link}} = grep { ! /$css->{xlink}/ } map { $_->{href} } $dom-> +find($css->{link})->each if ($css->{link});

      IMHO my post should have helped you as it showed that you can extend the expression in the grep codeblock.

      But now we know a little more of your problem and I assume the following: If $css->{xlink} is defined and not empty you want to try a match. The otherwise clause is not obvious to me, but you can change it. E.g.

      grep { $css->{xlink} && ! /$css->{xlink}/ }

      I hope I understood it right.

      McA

      I'm writing a scraper and sometimes I need to exclude some data. ...

      Well, that much (your desire) was clear from your original post, but its still not a question :)

      If you want to filter based on a regex, and a space isn't adequate, what is adequate?

      ?? if( sometingTrue ){ grep filter } else { no grep filter }

      Or were you after what McA wrote?

Re^2: Match nothing in grep { ! // }
by bart (Canon) on Mar 14, 2013 at 12:07 UTC
    The empty regex always matches
    Well, yes, and no. // is special, it serves as a shortcut, as if it's not the first regex you match, it signifies a repeat of the last regex that matched.

    perlreref:

    If 'pattern' is an empty string, the last successfully matched regex is used.

    In your tests here, there was no previous match, so this rule doesn't apply.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1023384]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2015-07-02 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (29 votes), past polls