I have a problem I don't understand: Given a string consisting of multiple lines like this:
DB<5> x $token
0 'ACCOUNT_CHANGED = "20250728081545Z"
ACCOUNT_CHANGED_T = "1753690545"
COMMON_NAME = "User Test"
CURRENT_TIME_T = "1753863424"
INSTANCE = "testing"
...
TEMPLATE_TIME_T = "1753699621"
USER_ID = "testuser"
WRAP_COLUMN = "999"'
I'm trying to process the strings line-by line using this code:
#...
while ($token && $token =~ /^([^\n]*?)(\n)(.*)$/m) {
$add_token->($1)
if (defined($1));
$add_token->($2);
}
$add_token->($token)
if (defined($token) && length($token) > 0);
#...
As it seems $3 is just set to be the second line instead of the rest of the string after the first line break.
Like this:
DB<4> x ($1, $2, $3)
0 'ACCOUNT_CHANGED = "20250728081545Z"'
1 '
'
2 'ACCOUNT_CHANGED_T = "1753690545"'
I have some questions, including a stupid one:
(.)*$, causing $3 to be the last character of the line following the newline.
Wouldn't * repeat the capture group, creating as many groups as there are characters on the line (so $3 should have been the first character instead of the last)?/m modifier?I quite don't understand:
As it seems $3 is just set to be the second line instead of the rest of the string after the first line break.
"Rest of the string after first line break" effectively means second line. And if you want to match "rest of string" you need to stop using .* as . won't match newline, unless you turn on single line mode /s (and yes, /m is perfectly fine, it just enables ^ and $ to match start and end of line, instead of entire string).
Here's regex with corrections Regex101.
Regarding question about (.)* not producing more capturing groups - this is because captured groups are normally numbered in the order of appearance in regex, not based on what they matched and how many times. So this pattern:
^([^\n]*?)(\n)(.)*$
has only 3 capture groups. Just the third one is overwritten each time there's next match for that group.