【问题标题】:C# replace by regular expressionC#用正则表达式替换
【发布时间】:2021-01-31 12:35:28
【问题描述】:

我有下面的 C# 代码来从字符串中删除停用词:

public static string RemoveStopWords(string Parameter)
{
      Parameter = Regex.Replace(Parameter, @"(?<=(\A|\s|\.|,|!|\?))($|_|0|1|2|3|4|5|6|7|8|9|A|about|after|all|also|an|and|another|any|are|as|at|B|be|because|been|before|being|between|both|but|by|C|came|can|come|could|D|did|do|does|E|each|else|F|for|from|G|get|got|H|had|has|have|he|her|here|him|himself|his|how|I|if|in|into|is|it|its|J|just|K|L|like|M|make|many|me|might|more|most|much|must|my|N|never|no|not|now|O|of|on|only|or|other|our|out|over|P|Q|R|re|S|said|same|see|should|since|so|some|still|such|T|take|than|that|the|their|them|then|there|these|they|this|those|through|to|too|U|under|up|use|V|very|W|want|was|way|we|well|were|what|when|where|which|while|who|will|with|would|X|Y|you|your|Z)(?=(\s|\z|,|!|\?))([^.])", " ", RegexOptions.IgnoreCase);
      return Parameter.Trim();
}

但是当我运行它时,它会在停用词不在字符串末尾时起作用,例如:

about this book 输出为book

manager only 输出为manager only

only manager 输出为manager

谁能指导一下?

【问题讨论】:

  • ([^.]) 我认为最后的那部分可能是你的问题。输入"manager only " 会发生什么? (注意最后的空格)
  • 当我们最后有空间时,例如“仅限经理”被替换为“经理”
  • [^.] 末尾的字符类期望出现单个字符。但是您使用积极的前瞻来断言直接在右边的是 ! ? , 一个空格字符或字符串的结尾。所以这部分([^.]) 也只能包含之前断言的内容,您可以省略前瞻并匹配它。您还可以通过使用字符类而不是使用| 来总结单个字符的所有替代方案,从而稍微缩短模式。
  • 例如(?&lt;=(?:\A|[\s.,!?]))(?:$|[A-Z0-9_]|about|after|all|also|and?|another|any|are|a[ts]|be|because|been|before|being|between|both|but|by|came|can|come|could|did|do|does|each|else|for|from|get|got|ha[ds]|have|her?|here|him|himself|his|how|i[nf]|into|i[st]|its|just|like|make|many|me|might|more|most|much|must|my|never|not?|now|o[fnr]|only|other|our|out|over|re|said|same|see|should|since|so|some|still|such|take|tha[tn]|the[nm]?|their|there|these|they|this|those|through|too?|under|up|use|very|want|wa[ys]|we|well|were|what|when|where|which|while|who|will|with|would|your?)(?=(?:\s|\z|[,!?]))
  • 非常感谢@the-fourth-bird,它工作得很好:)

标签: c# .net regex asp.net-core .net-core


【解决方案1】:

模式([^.]) 末尾的捕获组需要单个字符而不是点。 (?=(\s|\z|,|!|\?)) 之前的前瞻限制仅匹配列出的备选方案之一(它不能匹配已被前瞻排除的点)。

如果你想保留它,你可以省略那个前瞻,只匹配你允许匹配的东西,比如([\s,!?]|\z),但它仍然需要至少 1 个列出的替代方案。

您只能使用积极的前瞻,并将其更新为(?=[\s,!?]|\z)

(?<=\A|[\s.,!?])(?:$|[A-Z0-9_]|about|after|all|also|and?|another|any|are|a[ts]|be|because|been|before|being|between|both|but|by|came|can|come|could|did|do|does|each|else|for|from|get|got|ha[ds]|have|her?|here|him|himself|his|how|i[nf]|into|i[st]|its|just|like|make|many|me|might|more|most|much|must|my|never|not?|now|o[fnr]|only|other|our|out|over|re|said|same|see|should|since|so|some|still|such|take|tha[tn]|the[nm]?|their|there|these|they|this|those|through|too?|under|up|use|very|want|wa[ys]|we|well|were|what|when|where|which|while|who|will|with|would|your?)(?=[\s,!?]|\z)

.NET regex demo

关于模式的几点说明

  • 要缩短交替时间,您可以使用例如字符类 a[ts] 来匹配 atas 或使字符可选 and? 以匹配 anand
  • 在环视中,您不必添加其他分组机制,因此您可以使用(?=[\s,!?]|\z) 而不是(?=(?:[\s,!?]|\z))
  • 如果您不需要捕获组() 的值,您可以将它们设为非捕获(?:)
  • 数字1|2|3和字符A|B|C可以缩短为[A-Z0-9],也可以匹配下划线,你甚至可以缩短为\w

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    • 2022-06-12
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    相关资源
    最近更新 更多