gpt4 book ai didi

python - 正则表达式为英语和俄语提供不同的结果

转载 作者:行者123 更新时间:2023-11-28 17:30:35 24 4
gpt4 key购买 nike

所以,我有这个正则表达式:

[ ]{1}[^\w]*(шесть)[^\w]*[ ]{1}

及其变体:

[ ]{1}[^\w]*(conservation)[^\w]*[ ]{1}

我使用这个文本,here , 对其进行测试:

"""Наверное, по одному на пару отделений, а их больше десяти. Интересно, каждый работает по шесть часов в неделю? Работать, очевидно, некому, раз принимают сами заведующие. Но неужели экономия на нескольких диагностах"""

"""Following the assassination of President McKinley in September 1901, Roosevelt, at age 42, became the youngest United States President in history. Leading his party and country into the Progressive Era, he championed his "Square Deal" domestic policies, promising the average citizen fairness, breaking of trusts, regulation of railroads, and pure food and drugs. Making conservation a top priority, he established myriad new шесть national parks, forests, and monuments intended to preserve the nation's natural resources. In foreign policy, he focused on Central America, where he began construction of the Panama Canal. He greatly expanded the United States Navy, and sent the Great White Fleet on a world tour to project the United States' naval power around the globe. His successful efforts to end the Russo-Japanese War won him the 1906 Nobel Peace Prize."""

两者都是我发现的随机文本。但这不是重点。

当使用第一个正则表达式时,我得到以下匹配项:

по одному на пару отделений, а их больше десяти. Интересно, каждый работает по шесть часов в неделю? Работать, очевидно, некому, раз принимают сами заведующие. Но неужели экономия на нескольких

这是第一个文本 block ,俄文 block 。

在第二个中,它只匹配

шесть

匹配的上下文是

... new шесть national parks ...

如果我使用英文单词,比如“conservation”,它只会匹配英文文本 block 中的单词。

如果我将它添加到俄语文本中,类似于:

... шесть conservation часов ...

它匹配相同的大块文本,如“шесть”。

为什么会这样?是因为文字是俄语吗?

我不是百分百确定这个正则表达式的作用,但在英文文本中它会找到括号中的单词。我假设它对其他语言也是如此。

这并不重要,但仅供引用,我在 Python 2.7 中使用 re2 库。但是,由于我在网上得到了相同的结果,我假设它要么是我不理解的正则表达式,要么是非英语文本的问题。

谢谢!

编辑 1:

代码:

source = the_text_above
term = "шесть"
expression = regex_builder(term) # This returns the regex I posted
compiled_pattern = re2.compile(expression, re2.IGNORECASE, re2.U) # This raises an error: RegexError: pattern too large - compile failed
compiled_pattern.search(source).span() # This returns the start and end of the chunk of text I mentioned.

编辑 1 的附录:当我不使用 re2.U 时返回文本 block

编辑 2:

我也尝试过:

compiled_pattern = re.compile(expression, re.U)

我得到了相同的结果。

编辑 3 - 已解决:

因此,我尝试使用 re2.IGNORECASE 和 re2.U 标志再次编译并且成功了。

现在我的代码是这样的:

source = the_text_above
term = "шесть"
expression = regex_builder(term)
compiled_pattern = re2.compile(expression, re2.IGNORECASE | re2.U)
compiled_pattern.search(source).span()

它是这样工作的。

最佳答案

在 RE2 中,如果不指定 re2.U 标志,\w 仅匹配 ASCII 字母:

\w word characters (≡ [0-9A-Za-z_])

因此 [^\w] 匹配西里尔字母。

因此,您需要使用 re2.U 标志。

由于您将 re2.Ire2.U 组合在一起,因此您需要在两者之间使用按位或 (|):

re2.compile(<YOUR_PATTERN>, re2.I | re2.U) 

关于python - 正则表达式为英语和俄语提供不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34632702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com