In a Python Unicode string pattern, \W means a character outside Python’s word-character definition. It does not mean “anything that is not an Arabic letter,” and it does not reliably remove Arabic elongation. Tatweel, U+0640, is a word character in this setting. If the task is specifically to remove tatweel, remove that character explicitly.
A small reproducible case
import re
import unicodedata
word = "سـلام" # contains U+0640 TATWEEL
print(re.sub(r"\W+", "", word))
print(word.replace("\u0640", ""))
for char in ["ـ", "َ", "،", "1", "١", "_"]:
print(f"U+{ord(char):04X}", unicodedata.category(char),
bool(re.fullmatch(r"\w", char)))The first substitution preserves the tatweel; the explicit replacement produces سلام. The character-level inspection makes the assumptions visible, instead of guessing from how a glyph looks. The example is for Python str patterns, not byte patterns or every other regular-expression engine.
Marks, punctuation and digits behave differently
| Character | Code point / category | Matches \w? |
|---|---|---|
| ـ tatweel | U+0640 / Lm | Yes |
| َ fatha | U+064E / Mn | No |
| ، Arabic comma | U+060C / Po | No |
| 1 | U+0031 / Nd | Yes |
| ١ | U+0661 / Nd | Yes |
| _ | U+005F / Pc | Yes |
Replacing every \W character with an empty string also removes whitespace, potentially joining words, and can remove marks you wanted to preserve. Keeping \w keeps letters from other scripts and numbers as well. Decide which transformation the application actually needs before writing its filter.
Remove tatweel without silently redesigning the text
def remove_tatweel(text: str) -> str:
return text.replace("\u0640", "")
assert remove_tatweel("سـلام يا عالم") == "سلام يا عالم"
assert remove_tatweel("سَلَام") == "سَلَام"
assert remove_tatweel("١٢٣، test_1") == "١٢٣، test_1"These original cases deliberately preserve spaces, vocalization, punctuation, digits and Latin text. They test a narrow operation, not a complete Arabic normalization policy. Do not silently remove all combining marks from multilingual text: some are significant to spelling and meaning.
A Unicode block is not a language detector
A range such as [\u0600-\u06FF] includes the code points in that block; it is neither “all Arabic letters everywhere” nor “text written in the Arabic language.” Arabic-script letters also serve other languages, and relevant characters occur outside that single block. Python’s standard re module does not implement a \p{Script=Arabic} escape. A different engine’s property syntax must not be pasted into it as though it did.
For a larger cleaning task, keep an explicit policy and examples for tatweel, marks, digit conversion, whitespace and presentation forms. Make each transformation separately testable and retain the original text. The saved draft’s broad use of word-character filtering has been replaced with this scoped, reproducible explanation.
