How can I delete perl regex lines?

when i run:

perl -e '$x="abc\nxyz\n123"; $x =~ s/\n.*/... multiline.../; printf("str %s\n", $x);'

I expect the result to be as follows:

str abc... multiline...

instead i get

str abc... multiline...
123

Where am I going wrong?

+3
source share
2 answers
$x =~ s/\n.*/... multiline.../s
Modifier

/stells Perl to treat the matched string as single-line, which results in .matching newlines. This usually does not lead to observable behavior.

+7
source

You need to use the 's' modifier for your regular expression, so dot '.' will match any subsequent newline. So:

$x =~ s/\n.*/... multiline.../;

Becomes as follows:

$x =~ s/\n.*/... multiline.../s;
+2
source

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


All Articles