It is meant to match equation like expressions, to capture the key and values separately. Imagine you have a statement like height="30px"
, and you want to capture the height
attribute name, as well as its value 30px
.
So you have m|/([^=]+)="(.+)"|
.
The key is supposed to be everything before the =
is encountered. So [^=]
captures it. The ^
is a negation metacharacter when used as the first character inside []
brackets. It means that it will match any character except =
, which is what you want. The /
is probably a mistake, if you need to capture the group, you should not use it, or if it is indeed intended, it means to literally match an opening parentheses. Since it is a special character, it needs to be escaped, that's why \(
. if you mean to capture the group, it should be ([^=]+)
.
Next comes the =
sign, which you don't care about. Then the quotes which contain the value. So you capture it like "(.+)"
. the .+
will go on matching greedily every character, including the final "
. But then it will find that it can't match the final "
in the regex, so it will backtrack, give up the last "
the regex (.+)
captured, so that leaves the string within the quotes to be captured in the group. Now you are ready to access the key and value through $1
and $2
. Cool, isn't it?