正则表达式中(?s)与(?m)的区别

理论:

(?m) 和 (?s) 是正则表达式中的两个模式标志,它们具有不同的作用:

  1. (?m) 多行模式标志(也称为 "multiline" 模式):
  • 默认情况下,正则表达式将整个输入字符串视为单行
  • 多行文本中使用该标志时,正则表达式会匹配每一行
  1. (?s) 单行模式标志(也称为 "dotall" 模式):
  • 默认情况下,. 元字符匹配除了换行符之外的任意字符。
  • 当使用 单行模式标志时,. 元字符将匹配包括换行符在内的任意字符。

实践:

复制代码
import re

pattern1 = r'^.*'  
pattern2 = r'(?m)^.*'  
pattern3 = r'(?s)^.*'

matches1 = re.findall(pattern1, "Hello\nWorld")
matches2 = re.findall(pattern2, "Hello\nWorld")
matches3 = re.findall(pattern3, "Hello\nWorld")

print(matches1)  # 输出:['Hello']
print(matches2)  # 输出:['Hello', 'World']
print(matches3)  # 输出:['Hello\nWorld']
相关推荐
Apple_羊先森1 小时前
ORACLE数据库巡检SQL脚本--22、检查碎片程度最高的业务表
数据库·sql·oracle
OnYoung2 小时前
更优雅的测试:Pytest框架入门
jvm·数据库·python
山岚的运维笔记3 小时前
SQL Server笔记 -- 第85章:查询提示
数据库·笔记·sql·microsoft·sqlserver
chilavert3183 小时前
技术演进中的开发沉思-371:final 关键字(中)
java·前端·数据库
tryCbest3 小时前
SQL Server数据库
数据库·sql server
_codemonster4 小时前
PreparedStatement 和 Statement的区别
数据库·oracle
恒云客4 小时前
python uv debug launch.json
数据库·python·json