不使用 `\s` 和 `\S` 的原因 [英] Reasons for not using `\s` and `\S`

查看:48
本文介绍了不使用 `\s` 和 `\S` 的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用正则表达式扩展 \s 匹配空白字符的 GNU sed 答案及其匹配非空白字符的双 \S 似乎被回避了.这是为什么?

In GNU sed answers using the regular expression extensions \s which matches whitespace caracters and its dual \S which matches non-whitespace characters, seem to be shied from. Why is this?

推荐答案

它们仅支持 GNU,因此不可移植到其他 sed,仅此而已.只需匹配空格并使用 -E(其余的 idk),您引用的示例 将适用于 OSX/BSD sed 或 GNU sed,但如果您使用 \s 而不是 [ \t] 然后它会停止在 OSX/BSD sed 中工作.就个人而言,我会使用 [[:space:]] 来实现稳健性和可移植性.

They're GNU-only and so non-portable to other seds, that's all. Just wrt matching white space and use of -E (idk about the rest), the example you referenced would work with OSX/BSD sed or GNU sed as written, but if you used \s instead of [ \t] then it'd stop working in OSX/BSD sed. Personally I'd have used [[:space:]] for robustness and portability.

这篇关于不使用 `\s` 和 `\S` 的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆