Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to match against lines that contain a checkmark in them: ✓

I'm using python3.

The unicode encoding can be read about here: https://codepoints.net/U+2713?lang=en

The line I'm trying to match against looks like this:

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

re.match("✓", line) does not work. re.match("/u2713", line) does not work either.

How can I determine if the line contains a ✓?

--- UPDATE ---

solved: apparently there was an invisible character of some sort preceding the ✓ and this caused the match operator to fail. Thanks to @NickT and @EricDuminil for clueing me in. Also, the in operator appears to be easier and safer, so I'm marking that answer as correct.

like image 506
tadasajon Avatar asked Apr 16 '26 18:04

tadasajon


2 Answers

You don't even need any regex. You could use the in operator:

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

If you want to display the lines with a checkmark inside 'marks.txt', you can write:

with open('marks.txt') as f:
    for line in f:
        if "✓" in line:
            print(line, end='')
like image 85
Eric Duminil Avatar answered Apr 18 '26 07:04

Eric Duminil


For a fool-proof method, specify the character by name:

>>> line = '✓ Chrome on MAC - MySite.com - version-1'
>>> re.match('\N{CHECK MARK}', line)
<_sre.SRE_Match object; span=(0, 1), match='✓'>
like image 36
wim Avatar answered Apr 18 '26 08:04

wim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!