【发布时间】:2017-06-28 19:26:50
【问题描述】:
我正在寻找一个匹配这两个示例的正则表达式:
Hello I am really him.
Hello I am him.
一般来说,你如何设置一个正则表达式来匹配一个字符串,以及该字符串中缺少一个单词的子集?
更新:
所以这只是一个例句,看看我是否可以在我的实际字符串上重现相同的模式。然而,这并不完全奏效,所以他们在这里。 我需要匹配句子:
Ulcerative colitis patient, biopsy taken from the descending colon, macroscopic inflammation vissible
descending 这个词可能出现也可能不出现,no 这个词可能出现在最后一个逗号之后。所以它可能是:
Ulcerative colitis patient, biopsy taken from the descending colon, macroscopic inflammation vissible
Ulcerative colitis patient, biopsy taken from the descending colon, no macroscopic inflammation vissible
Ulcerative colitis patient, biopsy taken from the colon, macroscopic inflammation vissible
Ulcerative colitis patient, biopsy taken from the colon, no macroscopic inflammation vissible
【问题讨论】:
-
类似
Hello I am(?: really)? him. -
我不明白这篇文章。我以为您将原始示例用作某种模板,而不是逐字逐句的文本、间距和大写字面句子。文本中没有什么是静态的!你最好使用几个单词文字穿插
.*?来获得匹配。 -
@sln 在大多数情况下它是静态的。唯一改变的是
descending和no这两个词的存在(或不存在) -
如果是一个句子,那很好,但如果它是多个不同的句子,你就很难过。
-
注意错字:“vissible”应该是“visible”。