Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^3: Regex solution needed

by spivey3587 (Acolyte)
on Feb 23, 2007 at 19:47 UTC ( #601808=note: print w/replies, xml ) Need Help??

in reply to Re^2: Regex solution needed
in thread Regex solution needed

Thanks for all the input, guys. I enhanced the initial suggestion to include a few more lookforward possibilities which I think covers enough bases for me to keep it in the dictionary. I omitted the dirty test cases since these are "sacred" boards, but they did get caught correctly :)

my @tests = ( "How bout them cocks?", "I'm a big cocks fan", "I love the cocks", "That cocks game was sweet", "Anyone know the cocks score from last night?", "gamecocks rule", "I love the gamecocks, but...", "My favorite cocks player is..." ); foreach my $s (@tests) { if ($s =~ / (?<! \b a ) (?<! \b the ) (?<! \b them ) (?<! \b game ) (?<! \s ) \s* cocks? \b (?! \s fan ) (?! \s game ) (?! \s score ) (?! \s player ) /x) { print "Vulgar: '$s'\n"; } }

Replies are listed 'Best First'.
Re^4: Regex solution needed
by ikegami (Patriarch) on Feb 23, 2007 at 20:13 UTC

    The \s should probably be \s+, and

    (?! \s+ fan ) (?! \s+ game ) (?! \s+ score ) (?! \s+ player )
    is slower than
    (?! \s+ (?: fan | game | score | player ) )

    This factors out the constant \s+, and it uses | which probably has a lower overhead than (?!...). Furthermore, alternations of constant strings can be highly optimized by re engine modifications demerphq added to 5.9. (I don't think those particular strings can be optimized, though.)

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2023-06-03 17:56 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (16 votes). Check out past polls.