简体   繁体   English

用于匹配 python 中的德语字符的正则表达式

[英]regex for matching german characters in python

Could someone help me on regex to match German words/sentences in python?有人可以帮助我使用正则表达式来匹配 python 中的德语单词/句子吗? It does not work on jupyter notebook.它不适用于 jupyter 笔记本。 I tried same in jsfiddle it works fine.我在 jsfiddle 中尝试了同样的方法,它工作正常。 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:所以最后,要么删除反斜杠以获得正确的字符 class ,如:

[^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.所以要么使用像\w+这样简单的东西,要么使用@Wiktor在评论部分提出的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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