通过 pyperclip 多行复制文本搜索正则表达式问题
Regex problem searching through a pyperclip multipleline copied text
如果搜索表达式涉及 \n
换行符,尝试通过 pyperclip.paste()
进行正则表达式搜索时,我遇到了一件罕见的事情。
请原谅我的英语。
在搜索时,我通过分配给 text
变量的三重引号实现了搜索:
import re
text = '''
This as the line 1
This as the line 2
'''
pattern = re.compile(r'\d\n\w+')
result = pattern.findall(text)
print(result)
它实际上打印了换行符\n
。这就是我想要的,或者几乎是我所期望的。
»»» ['1\nThis']
但是当要搜索的字符串来自从剪贴板复制的文本时,问题就出现了。
This as the line 1
This as the line 2
假设我只是 select 并将该文本复制到剪贴板,我希望正则表达式从中提取相同的先前输出。
这次需要用到pyperclip模块
所以,忘记之前的代码,改写这个:
import re, pyperclip
text = pyperclip.paste()
pattern = re.compile(r'\d\n\w+')
result = pattern.findall(text)
print(result)
这是结果:
»»» [ ]
只有两个括号。我发现(在我的经验不足中)导致这个问题的是 \n
字符。它与 python (也是 \n 字符)之间的冲突无关,因为我们用 'r'.
避免了这种情况
我已经找到了一个不太清楚的解决方案(对我来说差不多,因为我现在只是 python 的基础)。
import re, pyperclip
text = pyperclip.paste()
lines = text.split('\n')
spam = ''
for i in lines:
spam = spam + i
pattern = re.compile(r'\d\r\w+')
result = pattern.findall(spam)
print(result)
请注意,我没有选择 \n
来检测最后一个正则表达式中的 新行 ,而是选择 \r
(\n
会导致同样的不良行为只打印括号)。
\r
可与 \s
交换,输出有效,但是:
»»» ['1\rThis']
用\r
代替\n
至少这对我来说是一个小小的胜利。
如果你能向我解释一个更好的解决方案,那将对我有很大帮助我几乎明白为什么会这样。你也可以推荐我一些概念来研究,以充分理解这一点。
粘贴时出现 \r
的原因是因为您是从 Windows 机器粘贴的。在 windows 上,换行符由 \r\n
表示。请注意 \s
不同于 \r
。 \s
表示任何空白字符。 \r
只是回车return字符。
正文:
This as the line 1
This as the line 2
实际看起来像:
This as the line 1\r\n
This as the line 2\r\n
在 windows 机器上。
在正则表达式中,\d\r
匹配到第一行的末尾:1\r
但是 \w+
与 \n
不匹配。您需要将第一个正则表达式编辑为:
pattern = re.compile(r'\d\r\n\w+')
来源:Do line endings differ between Windows and Linux?
如果搜索表达式涉及 \n
换行符,尝试通过 pyperclip.paste()
进行正则表达式搜索时,我遇到了一件罕见的事情。
请原谅我的英语。
在搜索时,我通过分配给 text
变量的三重引号实现了搜索:
import re
text = '''
This as the line 1
This as the line 2
'''
pattern = re.compile(r'\d\n\w+')
result = pattern.findall(text)
print(result)
它实际上打印了换行符\n
。这就是我想要的,或者几乎是我所期望的。
»»» ['1\nThis']
但是当要搜索的字符串来自从剪贴板复制的文本时,问题就出现了。
This as the line 1
This as the line 2
假设我只是 select 并将该文本复制到剪贴板,我希望正则表达式从中提取相同的先前输出。 这次需要用到pyperclip模块
所以,忘记之前的代码,改写这个:
import re, pyperclip
text = pyperclip.paste()
pattern = re.compile(r'\d\n\w+')
result = pattern.findall(text)
print(result)
这是结果:
»»» [ ]
只有两个括号。我发现(在我的经验不足中)导致这个问题的是 \n
字符。它与 python (也是 \n 字符)之间的冲突无关,因为我们用 'r'.
我已经找到了一个不太清楚的解决方案(对我来说差不多,因为我现在只是 python 的基础)。
import re, pyperclip
text = pyperclip.paste()
lines = text.split('\n')
spam = ''
for i in lines:
spam = spam + i
pattern = re.compile(r'\d\r\w+')
result = pattern.findall(spam)
print(result)
请注意,我没有选择 \n
来检测最后一个正则表达式中的 新行 ,而是选择 \r
(\n
会导致同样的不良行为只打印括号)。
\r
可与 \s
交换,输出有效,但是:
»»» ['1\rThis']
用\r
代替\n
至少这对我来说是一个小小的胜利。
如果你能向我解释一个更好的解决方案,那将对我有很大帮助我几乎明白为什么会这样。你也可以推荐我一些概念来研究,以充分理解这一点。
粘贴时出现 \r
的原因是因为您是从 Windows 机器粘贴的。在 windows 上,换行符由 \r\n
表示。请注意 \s
不同于 \r
。 \s
表示任何空白字符。 \r
只是回车return字符。
正文:
This as the line 1
This as the line 2
实际看起来像:
This as the line 1\r\n
This as the line 2\r\n
在 windows 机器上。
在正则表达式中,\d\r
匹配到第一行的末尾:1\r
但是 \w+
与 \n
不匹配。您需要将第一个正则表达式编辑为:
pattern = re.compile(r'\d\r\n\w+')
来源:Do line endings differ between Windows and Linux?