I'm trying to shorten all multiple spaces to one space, except for the first occurence of spaces (indentation).
I've found that this code will replace the first occurences:
$_ =~ s/^ +/ /;
So I thought, its negation will do what I want. But it does not:
$_ =~ s/!^ +/ /g;
What am I doing wrong?
你可以改变正则表达式的方法
s/\S\K +/ /g;
Exclamation mark is not negation in regex. At least, not like that.
What you need is negative lookbehind:
s/(?<!^)\s+/ /g;
Should do the trick.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.