开发者

What does this Regex mean?

Can someone tell me what it's trying to match exactly开发者_JAVA技巧?

$exp = '/[\s]+col[\s]*=[\s]*"([^"]*)"/si';


You can write regular expressions with comments, if you add the /x modifier. so here is a lengthy and documented version (always advisable for complex ones):

$exp = '/
          [\s]+     # one or more spaces
          col       #       col
          [\s]*     # zero or more spaces
          =         #        =
          [\s]*     # spaces
          "         #        "
          ([^"]*)   # anything but " and zero or more of it
          "         #        " 
    /six';

Also you sometimes see [^<">] in place of [^"] to make such regexps more resilient against malformed html.


It appears to be matching  col="some value", while being very forgiving of whitespace around the equals sign, being case insensitive, and regardless of whether the value is empty or not.

On a side note, it's curious what the s modifier is doing there since there are no . metacharacters.


I think others have already gave a good answer. As an aside, if this is not something for parsing markup, then you could boost functionality on the string side with something like this:

\s+ col \s* = \s* "( (?: \\. | [^\\"]+ )* )"

Perl'ish would be:

use strict;
use warnings;

my $regex = qr/

    \s+ col \s* = \s* "( (?: \\. | [^\\"]+ )* )"

/sx;

my $string = q(
 col  =  " this'' is \" a test\s,
           of the emergency broadcast system,
           alright .\". cool."
);

if ( $string =~ /$regex/ )
{
     print "Passed  val =\n $1\n";

}
__END__

Passed  val =
  this'' is \" a test\s,
           of the emergency broadcast system,
           alright .\". cool.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜