Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试将所有多个空格缩短为一个空格,但第一次出现空格(缩进)除外。
我发现这段代码将取代第一次出现:
$_ =~ s/^ +/ /;
所以我想,它的否定会做我想做的事。但它没有:
$_ =~ s/!^ +/ /g;
我究竟做错了什么?
您可以更改正则表达式的方法
s/\S\K +/ /g;
感叹号不是正则表达式中的否定。至少,不是那样的。
你需要的是消极的向后看:
s/(?<!^)\s+/ /g;
应该做的伎俩。