Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

match a word with leading white space

Tags:

regex

perl

I have the following strings in array @stat:

  r>  10.12.44.0/24                             
  r>  10.11.48.0/24                               
  *>  10.15.49.0/24                              
  r>  10.16.53.0/24                               
  r>  10.14.59.0/24                              
  *>  10.18.63.0/24

I want match the one who have "*>". Note that there is whitespace before the *. I tried using the following, but it didn't work.

foreach (@stat) {
    if (/^\s\*\>/) { 
    # do something
    }
}

What did I miss?

like image 553
raindrop Avatar asked Sep 02 '25 10:09

raindrop


1 Answers

\s matches one whitespace character. What you posted actually has two leading spaces. Tthe following should do the trick:

foreach (@stat) {
    if (/^\s*\*>/) { 
        # do something
    }
}

If not, check what's actually in your array more carefully.

use Data::Dumper qw( Dumper );

{
    local $Data::Dumper::Useqq = 1;
    print(Dumper(\@stat));
}
like image 99
ikegami Avatar answered Sep 04 '25 05:09

ikegami