简体   繁体   中英

regex for matching german characters in python

Could someone help me on regex to match German words/sentences in python? It does not work on jupyter notebook. I tried same in jsfiddle it works fine. I tried using this below script but does not work

import re
pattern = re.compile(r'\[^a-zA-Z0-9äöüÄÖÜß]\\', re.UNICODE)

print(pattern.search(text))

Your expression will always fail:

\[^a-zA-Z0-9äöüÄÖÜß]\\

Broken down, you require

[   # literally
^   # start of the line / text
a-z # literally, etc.

The problem is that you require a [ literally right before the start of a line which can never be true (either there's nothing or a newline). So in the end, either remove the backslash to get a proper character class as in:

[^a-zA-Z0-9äöüÄÖÜß]+

But this will surely not match the words you're looking for (quite the opposite). So either use something as simple as \w+ or the solution proposed by @Wiktor in the comments section.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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