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

Re^2: Regex conditional match if previous match in same expression is true?

by radiantmatrix (Parson)
on Apr 09, 2007 at 17:56 UTC ( [id://608998]=note: print w/replies, xml ) Need Help??


in reply to Re: Regex conditional match if previous match in same expression is true?
in thread Regex conditional match if previous match in same expression is true?

I'm not interested in matching whitespace -- my examples just happen to have some. I would want the results listed as "desired" in the node even if all whitespace was removed.

<radiant.matrix>
Ramblings and references
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet
  • Comment on Re^2: Regex conditional match if previous match in same expression is true?

Replies are listed 'Best First'.
Re^3: Regex conditional match if previous match in same expression is true?
by kyle (Abbot) on Apr 09, 2007 at 18:23 UTC

    In that case, how about this:

    use Test::More; my %tests = ( 'hello' => 1, '{hello}' => 1, 'hello}' => 0, '{hello' => 0, 'oh {hello} there' => 1, 'oh {hello there' => 0, 'oh hello there' => 1, 'oh hello} there' => 0, ); plan 'tests' => scalar keys %tests; while ( my ( $text, $result ) = each %tests ) { my $hello = qr/hello/; my $test_result = ( $text =~ / \{$hello\} # hello with braces | # or (?<! \{ ) # not a brace $hello # hello (?! \} ) # also not a brace /x ) ? 1 : 0; is( $test_result, $result, $text ); }

    Put your (possibly complicated) match text in a variable so you don't have to change it in two places when it changes. After that, literally match the text with braces and without.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-25 08:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found