简体   繁体   中英

perl regex match using global switch

I am trying to match a word that starts with a letter and is followed by at .

I use this regex for it

use strict;
use warnings;

use Data::Dumper;

my $str = "fat 123 cat sat on the mat";

my @a = $str =~ /(\s?[a-z]{1,2}(at)\s?)/g;

print Dumper( @a );

the out put I am getting is:

$ perl ~/playground/regex.pl 
$VAR1 = 'fat ';
$VAR2 = 'at';
$VAR3 = ' cat ';
$VAR4 = 'at';
$VAR5 = 'sat ';
$VAR6 = 'at';
$VAR7 = ' mat';
$VAR8 = 'at';

why does it match "at" as well when I clearly say match just 1 character before at.

Your optional spaces aren't a good way to delimit words: they are optional

Use the word boundary construct \\b for a rough match to the ends of words

use strict;
use warnings;

use Data::Dumper;

my $str = "fat 123 cat sat on the mat";

my @aa = $str =~ /\b[a-z]+at\b/gi;

print Dumper \@aa;

output

$VAR1 = [
          'fat',
          'cat',
          'sat',
          'mat'
        ];

If you want to be more clever and be certain that the word found isn't preceded or followed by a non-space character then you can write this instead

my @aa = $str =~ /(?<!\S)[a-z]+at(?!\S)/gi;

which produces the same result for the data you show

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM