Python正则表达式re模块的相关知识积累与博文汇总

正则表达式的内容很多,也很灵活和强大,有必须做下记录,进行汇总。

01-初学Python的re模块的正则表达式的常用方法与常见问题记录

链接1:https://blog.csdn.net/wenhao_ir/article/details/125960370
链接2:https://blog.csdn.net/wenhao_ir/article/details/125921141

02- 正则表达式中+ 与 * 有啥区别?

https://blog.csdn.net/wenhao_ir/article/details/131289181

03-Python的正则表达式re模块的compile()方法有什么作用?

https://blog.csdn.net/wenhao_ir/article/details/132026895

04-正则表达式中的符号^和符号$分别是什么意思,有什么用?

符号^表示从一个字符串的起始位置开始匹配,而符号$表示从一个字符串的结尾处开始匹配,例如,正则表达式 hello$ 将匹配以 “hello” 结尾的字符串。
组合使用 ^$ ,可以确保正则表达式必须在整个字符串范围内进行完整的匹配。
举个例子,假设有以下字符串列表:

strings = ["hello world", "world hello", "hello", "hello world again"]

如果我们使用正则表达式 hello 进行匹配,它将匹配到所有包含 “hello” 子串的字符串:

  • “hello world”
  • “world hello”
  • “hello world again”

但如果我们使用正则表达式 ^hello$ 进行匹配,它将只匹配整个字符串内容为 “hello” 的字符串,即只有 “hello” 这个字符串会被匹配。

04-正则表达式中的小括号有什么用?

通常用来使逻辑更为清楚,比如在匹配中国大陆手机号的正则表达式中:
有括号的正则表达式:

r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$'

比无括号的正则表达式:

r'^13[0-9]|15[012356789]|17[678]|18[0-9]|14[57][0-9]{8}$'

逻辑上更为清晰,避免了看着一饼粘的效果。

05-正则表达式中的方括号[]有什么用?

链接:https://blog.csdn.net/wenhao_ir/article/details/132028092

06-正则表达式中的大括号-花括号{}有什么用?

链接:https://blog.csdn.net/wenhao_ir/article/details/132028331

猜你喜欢

转载自blog.csdn.net/wenhao_ir/article/details/132027668
今日推荐