如何使用正则表达式来获取一段文本中的任意字符包括换行符“
”呢?
匹配方法可以使用([sS]*)或([dD]+)或([wW]*)
其中*表示重复零次或更多次,+表示重复一次或更多次。
注意:如果匹配好几段包含各种特殊字符的话,建议用括号括起来一部分短字符,不用括号可能会由于个别字符转义而报错,比如我用Python运行脚本就报错 [python SyntaxError: EOL while scanning string literal]还匹配失败,所以这里[sS]*和[dD]+就用括号括起来了。
可以使用正则表达式在线测试工具验证,如下图:
![](https://upload-images.jianshu.io/upload_images/5977343-d873616f12d8d6ed.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/675/format/webp)
匹配结果.png
正则表达式常用字符释义如下表:
![](https://upload-images.jianshu.io/upload_images/5977343-02adf7a8426f033a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/870/format/webp)