在Windows上:
import re
import subprocess
output = subprocess.run(['python', '-m', 'black'], stderr=subprocess.PIPE)
stderr = output.stderr.decode()
assert re.match(r'No Path provided. Nothing to do \\U0001f634\r\n', stderr)
但是,在Linux上,我需要在两个地方更改正则表达式:
import re
import subprocess
output = subprocess.run(['python', '-m', 'black'], stderr=subprocess.PIPE)
stderr = output.stderr.decode()
assert re.match(r'No Path provided. Nothing to do \U0001f634\n', stderr)
我理解为什么我需要将\r\n
更改为\n
——这是因为carriage return
但是,为什么我需要将'\\U0001f634'
更改为'\U0001f634'
?有没有一种跨平台编写上述断言的方法
这里出现差异的原因可能是因为表情符号在Windows中显示为十六进制值,在Linux中显示为文字表情符号
这里的解决方案是通过在
\r
之后添加?
量词,将\r
作为可选字符进行匹配,并使用替换来匹配转义表情符号或文字表情符号:如果点是文字点,请考虑转义,否则,^ {CD3>}匹配任何字符,但行断线:
详细信息
No Path provided. Nothing to do
-literalNo Path provided. Nothing to do
文本(?:\\U0001f634|\U0001f634)
-要么是一个\
后跟U0001f634
要么是一个😀
表情符号\r?
-可选CR\n
-一个LF字符李>相关问题 更多 >
编程相关推荐