需要RegEx才能返回第一段或前n个单词

我正在寻找RegEx来返回段落中的第一个[n]单词,或者如果段落包含少于[n]个单词,则返回完整的段落。

例如,假设我最多需要前7个单词:

one two three four five, six seven eight nine ten.

ignore

我得到:

 one two three four five, six seven 

对包含少于请求的字数的段落的相同RegEx:

 

one two three four five.

ignore

简单回归:

 one two three four five. 

我对此问题的尝试产生了以下RegEx:

 ^(?:\

)((?:\w+\b.*?){1,7}).*(?:\

)

但是,这只返回第一个单词 – “one”。 它不起作用。 我觉得 。*? (在\ w + \ b之后)导致问题。

我哪里错了? 任何人都可以提出一个有效的RegEx吗?

仅供参考,我正在使用.Net 3.5的RegEX引擎(通过C#)

非常感谢

好的,完成重新编辑以确认新的“规范”:)

我很确定你不能用一个正则表达式做到这一点。 最好的工具肯定是HTML解析器。 我能用正则表达式得到的最接近的是两步法。

首先,用以下内容隔离每个段落的内容:

 

(.*?)

如果段落可以跨越多行,则需要设置RegexOptions.Singleline

然后,在下一步中,迭代您的匹配并在每个匹配的Group[1].Value上应用以下正则表达式Group[1].Value

 ((?:(\S+\s+){1,6})\w+) 

这将匹配由空格/制表符/换行符分隔的前七个项目,忽略任何尾随标点符号或非单词字符。

但是它会将由空格分隔的标签视为其中一个项目,即

 One, two three  four five six seven 

它只会匹配到six 。 我想那是正则表达式,没有办法解决这个问题。

  1. 使用HTML解析器获取第一段,展平其结构(即删除段落中的装饰HTML标记)。
  2. 搜索第n个空白字符的位置。
  3. 将子串从0到该位置。

编辑:我删除了第2步和第3步的正则表达式提议,因为它是错误的(感谢评论者)。 此外,HTML结构需要展平。

我有同样的问题,并将一些Stack Overflow答案组合到这个类中。 它使用HtmlAgilityPack,这是一个更好的工具。 呼叫:

  Words(string html, int n) 

得到n个单词

 using HtmlAgilityPack; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace UmbracoUtilities { public class Text { ///  /// Return the first n words in the html ///  ///  ///  ///  public static string Words(string html, int n) { string words = html, n_words; words = StripHtml(html); n_words = GetNWords(words, n); return n_words; } ///  /// Returns the first n words in text /// Assumes text is not a html string /// http://stackoverflow.com/questions/13368345/get-first-250-words-of-a-string ///  ///  ///  ///  public static string GetNWords(string text, int n) { StringBuilder builder = new StringBuilder(); //remove multiple spaces //http://stackoverflow.com/questions/1279859/how-to-replace-multiple-white-spaces-with-one-white-space string cleanedString = System.Text.RegularExpressions.Regex.Replace(text, @"\s+", " "); IEnumerable words = cleanedString.Split().Take(n + 1); foreach (string word in words) builder.Append(" " + word); return builder.ToString(); } ///  /// Returns a string of html with tags removed ///  ///  ///  public static string StripHtml(string html) { HtmlDocument document = new HtmlDocument(); document.LoadHtml(html); var root = document.DocumentNode; var stringBuilder = new StringBuilder(); foreach (var node in root.DescendantsAndSelf()) { if (!node.HasChildNodes) { string text = node.InnerText; if (!string.IsNullOrEmpty(text)) stringBuilder.Append(" " + text.Trim()); } } return stringBuilder.ToString(); } } } 

圣诞节快乐!