Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Regex OR'ing

by spectre (Scribe)
on Jul 28, 2000 at 02:39 UTC ( #24771=perlquestion: print w/replies, xml ) Need Help??
spectre has asked for the wisdom of the Perl Monks concerning the following question:

I'm checking a variable for a regex that may or may not be there, using the following:
$var=~/(?:regex(catch_this)|)/; # My regex or nothing I've also tried: $var=~/(?:regex(catch_this)|.{0})/; #My regex or very literally nothin +g It always matches my regex when it looks like this: $var=~/regex(catch_this)/;
And it always matches nothing when I use the former 2 examples.
Is there a way I can force it to look at the first expression rather than (As it appears to be doing) whichever comes first?


Replies are listed 'Best First'.
Re: Regex OR'ing
by tye (Sage) on Jul 28, 2000 at 03:13 UTC
    $var =~ /regex(catch_this)/ || $var =~ /.{0}/;

    Regex always prefer matches early in the string. For two matches starting at the same point in the string, regex prefers the one matching elements leftmost in an alternation, where alternations early in the regex are more important than those later. To break ties here, the regex prefers longer matches (or shorter matches for non-greedy elements).

    Whether my snippet of code is of any use to you probably depends on other details you didn't mention.

      Ah, to clarify, I'm writing a module for a harness, I dont actually have the option of doing it that way.

      The interface needs to be like the following:
      $this->regex('key','regex'); In this case : $this->regex('high_bidder','(?:Date . Time<.B>.*?1\.<.B><.FONT><.TD> +\s+<TD[^>]+><FONT[^>]+>(.*?)<.FONT>|)');

Re: Regex OR'ing
by tye (Sage) on Jul 28, 2000 at 07:16 UTC

    Well, I'm still not clear, but this might work:

    $var =~ /regex(catch_this)|$/

    I don't see why you need to match nothing at the start of the string so matching nothing only at the end of the string might fix your problem. You can use \Z instead of $ is you are paranoid about trailing newlines and don't care about not working on old versions of Perl.

      This is what I ended up doing - It works beautifully.
      Thanks for the suggestion

Re: Regex OR'ing
by Fastolfe (Vicar) on Jul 28, 2000 at 19:13 UTC
    Why not try something like this:
    $var =~ /^(?:regex(catch_this))?$/
    Anchor it at the start and end of the line (or however you want to anchor the regexp so as to determine where your "absent" or "present" boundaries are), and make the inner grouping optional. If it matches, $1 will either contain "catch this" or be undefined.

    This method is more useful when you want to try to match something (and the absense of something) in the middle of a line. You don't have to anchor it at the start and end of the line.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://24771]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2017-10-20 08:10 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (260 votes). Check out past polls.