繁体   English   中英

python - unicode正则表达式匹配 - 如何搜索复选标记? ✓

[英]python - unicode regex match - how do I search for the checkmark? ✓

我试图匹配其中包含复选标记的行:✓

我正在使用python3。

可以在这里阅读unicode编码: https//codepoints.net/U+2713?lang = en

我试图匹配的行看起来像这样:

✓ Chrome on MAC - MySite.com - version-1

re.match("✓", line)不起作用。 re.match("/u2713", line)也不起作用。

如何确定该line包含✓?

---更新---

解决 :显然在✓之前有某种不可见的字符,这导致match运算符失败。 感谢@NickT和@EricDuminil为我提供了线索。此外, in运算符似乎更容易和更安全,所以我将答案标记为正确。

你甚至不需要任何正则表达式。 你可以使用in operator

>>> "✓" in "✓ Chrome on MAC - MySite.com - version-1"
True
>>> "✓" in "Chrome on MAC - MySite.com - version-1"
False

如果要在'marks.txt'显示带有复选标记的行,可以写:

with open('marks.txt') as f:
    for line in f:
        if "✓" in line:
            print(line, end='')

对于防呆的方法,请按名称指定字符:

>>> line = '✓ Chrome on MAC - MySite.com - version-1'
>>> re.match('\N{CHECK MARK}', line)
<_sre.SRE_Match object; span=(0, 1), match='✓'>

如何确定该行是否包含✓?

例:

import re


text = '''
123 456 789
✓ 123 456 789
123 456 789
123 456 ✓ 789
123 456 789
'''

for m in re.finditer('^.*✓.*$', text, re.MULTILINE):
    print('line:', m.group(0))

打印:

line: ✓ 123 456 789
line: 123 456 ✓ 789

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM