# Filtering out Chinese, Korean, Japanese, Thai characters with Python Script Node or Rule Engine using regular expression

**URL:** <https://forum.knime.com/t/filtering-out-chinese-korean-japanese-thai-characters-with-python-script-node-or-rule-engine-using-regular-expression/61350>\
**Category:** KNIME Analytics Platform\
**Tags:** regex, python\
**Created:** [February 8, 2023, 10:17pm UTC](https://forum.knime.com/t/filtering-out-chinese-korean-japanese-thai-characters-with-python-script-node-or-rule-engine-using-regular-expression/61350 "2023-02-08T22:17:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xli](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/x/bc79bd/32.png) [@xli](https://forum.knime.com/u/xli)\
**Post date:** [February 8, 2023, 10:17pm UTC](https://forum.knime.com/t/filtering-out-chinese-korean-japanese-thai-characters-with-python-script-node-or-rule-engine-using-regular-expression/61350/1 "2023-02-08T22:17:26Z")

</div>

Hi all, I was trying to filter data records that contain any Asian characters and later on use a translator on them…I tried with Python Script with exact same code as what I do in Spyder, which works in Spyder but not in the Python Script in KNIME.

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/f/f/ff183d75c756777a274dae6b7ca8bbda047919a1.png)  
here is the code:  
output\_table\_1 = input\_table\_1.copy()  
def contain\_chinese(check\_str):  
for ch in check\_str:  
if ‘\u4e00’ \<= ch \<= ‘\u9fa5’:  
return True  
return False

def contain\_korean(check\_str):  
for ch in check\_str:  
if ‘\uac00’ \<= ch \<= ‘\ud7a3’:  
return True  
return False

def contain\_japanese(check\_str):  
for ch in check\_str:  
if ‘\u0800’ \<= ch \<= ‘\u4e00’:  
return True  
return False

output\_table\_1 = output\_table\_1[output\_table\_1[‘Customer Name’].apply(lambda x : contain\_chinese(x))]

And I also tried using regular expression but it doesn’t seems to work well neither…  
here (example for Japanese), it would give me results that totally does not contain Japanese characters: ^._([\u0800-\u4e00])._

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/1/3/1343015b1bd2dbf2492c89f808f8b268ed7f47a2.png)

Therefore I end by with filtering out all latin characters with this Rule Engine Row Filter but it would still give me latin characters  
here is what I put and i chose exclude TRUE matches: $Customer Name$ MATCHES “^[a-zA-Z0-9\_?@&.,，~()（）^:;/=+~'’ àáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ-]+” =\> TRUE

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/8/4/84a51846fd06dae96594f7fbcb68fbe035e565be.png)

If anyone can give some advice, it would be really appreciated!  
Thank you in advance!

---

<div class="post-metadata">

**Author:** ![mlauber71](https://forum-cdn.knime.com/user_avatar/forum.knime.com/mlauber71/32/73590_2.png) [@mlauber71](https://forum.knime.com/u/mlauber71)\
**Post date:** [February 8, 2023, 11:07pm UTC](https://forum.knime.com/t/filtering-out-chinese-korean-japanese-thai-characters-with-python-script-node-or-rule-engine-using-regular-expression/61350/2 "2023-02-08T23:07:37Z")

</div>

@xli you might want to check if you will have to double escape the Unicode codes.

> [@Weird Character Appearing](https://forum.knime.com/t/weird-character-appearing/47511/8):
>
> @badger101 a very strange case indeed. It you want to spot such things best copy them into an advanced editor like notepad ++ or visual studio code or something. You might have to escape the unicode string in the RegEx syntax with KNIME. regexReplace($$CURRENTCOLUMN$$, "[\\u200E]" , "") Another approach can be to eliminate all strange characters and only leave the ones you explicitly allow (there was a debate about this before): regexReplace($$CURRENTCOLUMN$$, "[^\\w\\d\\s.,!@#$%&\*()=+~-]","…

There are several other threads and examples about [RegEx](https://hub.knime.com/search?type=all&tag=regex&sort=best) that might help.

> [@How to deal with unicode / special / invisible characters?](https://forum.knime.com/t/how-to-deal-with-unicode-special-invisible-characters/48095/8):
>
> Hi @mwiegand Looking up this specific character, it falls into a class of unicode characters termed “Formatting”. I was recently investigating how to strip emoji and other characters from strings, and in the process discovered some quite useful info. In regex, there are a set of regex patterns describing unicode “classes” or “categories”. Once you know the class you wish to find or filter, you can then use these patterns. The pattern for this particular Formatting class is \p{Cf} , so if you …

---

<div class="post-metadata">

**Author:** ![xli](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/x/bc79bd/32.png) [@xli](https://forum.knime.com/u/xli)\
**Post date:** [February 9, 2023, 8:06am UTC](https://forum.knime.com/t/filtering-out-chinese-korean-japanese-thai-characters-with-python-script-node-or-rule-engine-using-regular-expression/61350/3 "2023-02-09T08:06:27Z")

</div>

> [@mlauber71](#):
>
> escape

hi thanks for your reply.  
I tried double escape but it still doesnt work. It’s not like it doesnt work at all, with the rule engine row filter, I did filtered some records with Asian form, but it would also give records that are all in English.

---

<div class="post-metadata">

**Author:** ![xli](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/x/bc79bd/32.png) [@xli](https://forum.knime.com/u/xli)\
**Post date:** [February 9, 2023, 8:06am UTC](https://forum.knime.com/t/filtering-out-chinese-korean-japanese-thai-characters-with-python-script-node-or-rule-engine-using-regular-expression/61350/4 "2023-02-09T08:06:57Z")

</div>

hi thanks for your reply.  
I tried double escape but it still doesnt work. It’s not like it doesnt work at all, with the rule engine row filter, I did filtered some records with Asian form, but it would also give records that are all in English.

---

<div class="post-metadata">

**Author:** ![system](https://forum-cdn.knime.com/uploads/default/original/1X/ab3ccf34482a0329361734a18199390177204f15.png) [@system](https://forum.knime.com/u/system)\
**Post date:** [May 10, 2023, 8:07am UTC](https://forum.knime.com/t/filtering-out-chinese-korean-japanese-thai-characters-with-python-script-node-or-rule-engine-using-regular-expression/61350/5 "2023-05-10T08:07:12Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
