tags:

views:

133

answers:

4

Can anybody tell me what pattern do I need to write in the bind expression if I want to check whether the string contains aaa or aab or a?

$str = "Hello how are aaa you?";
print $str =~ m/what should i write here?/;
+7  A: 
$str =~ /a|aaa|aab/

You might want to check the regular operator precedence to make sure you don’t get caught by things like /^foo|bar$/ (matches ^foo or bar$, contrast with ^(foo|bar)$).

zoul
You only need the first alternation there. You've never visited the other ones.
brian d foy
I assumed that the strings given were just examples and the poster wants to match any string alternation in general. Otherwise I’d have hard time imagining the use case for `/a|aaa|aab/`.
zoul
That's why you can use more than code to explain your answer. Despite what anyone says, code does not speak for itself, especially for people who don't know how to do it already.
brian d foy
+3  A: 

(aaa|aab|a)...

Oli Charlesworth
+5  A: 
$str =~ /a/

(Think about it...)

mscha
A: 

Given mscha's point (answer not comment) I wonder if you really want to match and keep. If you want to know what you found try something like a global match captured to an array.

#!/usr/bin/perl

use strict;
use warnings;

my $str = "Hello how are aaa you?";
my @found = $str =~ m/(aaa|aab|a)/g;
foreach my $result (@found) {
  print "found $result\n";
}
Joel
Really? What's the downvote for? We seem to be debating the OP as all he needs is to search for the letter 'a' and so I suggest that perhaps the OP wants to know which of the strings was matched. What was so wrong with that?
Joel