Nathan
Nathan

Reputation: 45

Filter string with non-English character in Python

I have this list for example

items = ['www1bmobạnk.com', 'наш-переславль.рф', 'вольттек.рф', '別邸福の花浜松町店.com', 'благовест.рус', 'թարմլուր.հայ', '피시방.com', 'ått.com', '沃華科技.com', 'впамяти.рф', 'андрейбабкин.рф', '꽃셰프가.com', 'фортуна36.рф', 'わかば.com', 'тесты-на-коронавирус.рф', '第一個夏天.com', 'bëstchange.net', 'normaldomain.com']

I'm using re.match to filter them out

for item in items:
    if re.match('[a-z0-9]', item):
        print("It's English! " + item)
    else:
        print("It's not! " + item)

The issue is bëstchange.net and www1bmobạnk.com didn't get filtered. I added [^ë] and it worked for bëstchange.net, but [^ạ] didn't work for www1bmobạnk.com. I also try Unicode [\u0061-\u007A], but it's pretty much the same thing.

Appreciate any suggestions!

Upvotes: 2

Views: 659

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626952

You can check if a string contains a letter that is not an ASCII letter:

import re
items = ['www1bmobạnk.com', 'наш-переславль.рф', 'вольттек.рф', '別邸福の花浜松町店.com', 'благовест.рус', 'թարմլուր.հայ', '피시방.com', 'ått.com', '沃華科技.com', 'впамяти.рф', 'андрейбабкин.рф', '꽃셰프가.com', 'фортуна36.рф', 'わかば.com', 'тесты-на-коронавирус.рф', '第一個夏天.com', 'bëstchange.net', 'normaldomain.com']
for item in items:
    if not re.search(r'(?![a-zA-Z])[^\W\d_]', item):
        print(f"It's English! {item}")
    else:
        print(f"It's not! {item}")

See the Python demo. Only normaldomain.com passes the test now.

The (?![a-zA-Z])[^\W\d_] pattern matches any Unicode letter (with [^\W\d_]) but the (?![a-zA-Z]) negative lookahead "tempers", restricts this pattern so that it could not match an ASCII letter.

Upvotes: 1

Related Questions