perl - Check if a patteren contains more than. one string( type array) -


how check if pattern array( contains more 1 string) or single string?

consider following example,

if ( $_ =~ /#define\s+(\s+)\s*(.*)/gi )  ..  #check if $2 array or single string  if ( $2  

if it's array split them patterns else keep ?

please suggest.

regards, div

a few basic rules , terminology. regex match done on string, in single-valued variable, scalar. array variable associated collection of items, representing list of values. perhaps can start perl-variable-types in perlintro, , regex perlretut.

in example show, variable $_ contains string checked pattern.

the match operator returns list of matches captured parentheses in pattern. /g modifier behavior more complex, , depends on whether regex used in scalar or list context. see under regexp-quote-like-operators in perlop. in short, assign list of matches array (no need /g then)

my @matches = $_ =~ /#define\s+(\s+)\s*(.*)/i; 

and can check number of elements in array

if (@matches == 0) {     print "there no matches\n"; } elsif (@matches == 1) {     print "found 1 match: $matches[0]\n"; } else {     print "found more one: @matches\n"; } 

another way check empty array if (not @matches).

this captures word \s+ , (possible) remainder of line .*, when preceded #define space. note regex allows come anywhere in string. if, in fact, must @ beginning add anchor optional space @ beginning, /^\s*.../


Comments

Popular posts from this blog

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

laravel - Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View: F:\project\resources\views\admin\carousels\index.blade.php) -