【问题标题】:regex doesn't match last word正则表达式与最后一个单词不匹配
【发布时间】:2018-10-01 21:00:53
【问题描述】:

我有这个简单的正则表达式:

RegEx_Seek_1 := TDIPerlRegEx.Create{$IFNDEF DI_No_RegEx_Component}(nil){$ENDIF};
s1 := '(doesn''t|don''t|can''t|cannot|shouldn''t|wouldn''t|couldn''t|havn''t|hadn't)';
// s1 contents this text: (doesn't|don't|can't|cannot|shouldn't|wouldn't|couldn't|havn't|hadn't)
RegEx_Seek_1.MatchPattern := '(*UCP)(?m)'+s1+' (a |the )(ear|law also|multitude|son)(?(?= of)( \* | \w+ )| )([^»Ô¶ ][^ »Ô¶]\w*)';

针对冠词找名词,后面可以跟of。如果有of,那么我需要搜索名词\w+(还有\*;动词的替代品)。最后一个词应该是动词。

示例文本:

. some text . Doesn't the ear try ...
. some text doesn't the law also say ...
. some text doesn't the son bear ...
. some text . Shouldn't the multitude of words be answered? ...
. some text . Why doesn't the son of * come to eat ... 

我的结果:

Doesn't the ear try
doesn't the law also say
doesn't the son bear
Shouldn't the multitude of words

而且它没有得到最后一句话: doesn't the son of * come

我的计划是在最后一个词前加上\K来得到动词。

字符的排除: [^»Ô¶] 是因为»、Ô、 已经在文本中代表了一些标记,以描述现有的动词。它们可能存在也可能不存在。我正在使用空格。制表符是分隔符,不是任何句子的一部分。

在这个正则表达式中,我添加了一个空格 [^»Ô¶ ] 来获得最后一个字。

所以问题是如何更正正则表达式以获得更多行: doesn't the son of * come

编辑:

我需要在替换时引用同一组中的动词(我将引用动词)。

【问题讨论】:

  • @Wiktor Stribiżew:抱歉,我错过了在 words 之后添加 be answered?。这意味着,如果您添加 take 句子 Shouldn't the multitude of words be answered? ,则会捕获单词而不是 be。这就是为什么我在那里有 if 条件。我需要在替换时引用同一组中的动词。
  • 此刻这句话太过分了。您应该始终考虑Shouldn't the multitude of words have been answered?、Shouldn't the multitude of words in this letter of my friend be answered? 等情况。不确定是否可以在不列出所有可能要匹配的单词形式的情况下使用正则表达式来做到这一点。

标签: regex delphi


【解决方案1】:

你的错误在(?(?= of)( \* | \w+ )| )。

请记住,前瞻不会向前移动光标,因此( \* | \w+ ) 将匹配of ,所以现在余数是* come,它不能被([^»Ô¶ ][^ »Ô¶]\w*) 匹配,因为第二个字符是空格.

我猜你应该匹配已经处于你状态的 of,比如 (?(?= of) of( \* | \w+ )| )

【讨论】:

    【解决方案2】:

    我修改了 Wiktor 的模式以匹配:

    (*UCP)(?m)'+s1+' (a |the )(ear|law also|multitude|son)(?:\s+of Words|\s+of \*)*\s+\K(?P<verb>[^\s»Ô¶]+)
    

    现在我可以像这样引用最后一组:

    char(182)+'$<verb>'
    

    我展示了如何使用 TDIRegEx 的 Replace2 函数更改动词的结果。你看它有效:

    为什么*的儿子¶来吃饭

    难道耳朵¶试试的话,

    为什么儿子¶承受

    难道法律不也¶说同样的事情吗?

    难道不应该¶被回答大量的词吗?

    Wictor 和 Sebastian 的两个答案都帮助我解决了这个问题。谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-08
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多