What does this regular expression mean?

Can someone tell me what he is trying to match exactly?

$exp = '/[\s]+col[\s]*=[\s]*"([^"]*)"/si'; 
+4
source share
3 answers

You can write regular expressions with comments if you add the /x modifier. so here is a long and documented version (always recommended 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 [^<">] instead of [^"] to make such regular expressions more resistant to distorted html.

+3
source

It seems to match col="some value" , being a very forgiving space around the equal sign, case insensitive and whether it is empty or not.

On a side note, it’s curious what the s modifier does there, since it is metacharacters . are absent.

+3
source

I think others have already given a good answer. Aside, if this is not something to parse the markup, then you can increase the functionality on the line side with something like this:

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

Perl'ish will 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. 
+1
source

Source: https://habr.com/ru/post/1333843/


All Articles