简体   繁体   English

用于执行字符串函数的任务的正则表达式

[英]Regular expression for performing task being done by string functions

The below code is performing following functionality which I intend to integrate into larger application. 以下代码执行以下功能,我打算将其集成到更大的应用程序中。

  1. Splitting large input string input by dot (.) character wherever it occurs in input string. 分割大的输入字符串input通过无论它在输入串发生点(。)的字符。
  2. Storing the splitted substrings into array result[] ; 将分裂的子串存储到数组result[] ;
  3. In the foreach loop , a substring is matched for occurrence of keyword . 在foreach循环中,匹配子字符串以发生关键字
  4. If match occurs , starting from position of this matched substring in original input string , upto 300 characters are to be printed. 如果发生匹配,则从原始输入字符串中此匹配子字符串的位置开始,最多打印300个字符

      string[] result = input.Split('.'); foreach (string str in result) { //Console.WriteLine(str); Match m = Regex.Match(str, keyword); if (m.Success) { int start = input.IndexOf(str); if ((input.Length - start) < 300) { Console.WriteLine(input.Substring(start, input.Length - start)); break; } else { Console.WriteLine(input.Substring(start, 300)); break; } } 

The input is in fact large amount of text and I think this should be done by regular expression. 输入实际上是大量的文本,我认为这应该通过正则表达式来完成。 Being a novice ,I am not able to put everything together using a regular expressions . 作为一个新手,我无法使用正则表达式将所有内容放在一起。

Match keyword. 匹配关键字。 Match m = Regex.Match(str, keyword);

300 characters starting from dot (.) ie starting from matched sentence , print 300 characters "^.\\w{0,300}" 从点(。)开始的300个字符,即从匹配的句子开始,打印300个字符"^.\\w{0,300}"

What I intend to do is : 我打算做的是:

  1. Search for keyword in input text. 在输入文本中搜索关键字。

  2. Just as a match is found , start from the sentence containing the keyword and print upto 300 characters from input string. 就像找到匹配项一样, 从包含关键字的句子开始,从输入字符串打印最多300个字符。

    How should I proceed ? 我该怎么办? Please help . 请帮忙 。

If I got it right, all you need to do is find your keyword and capture all that follows until you find first dot or reach maximum number of characters: 如果我做对了,您需要做的就是找到您的keyword并捕获所有keyword ,直到您找到第一个点或达到最大字符数:

@"keyword([^\.]{0,300})"

See sample demo here . 请参阅此处的示例演示

C# code: C#代码:

var regex = new Regex(@"keyword([^\.]{0,300})");

foreach (Match match in regex.Matches(input))
{
   var result = match.Groups[1].Value;

   // work with the result
}

Try this regex: 试试这个正则表达式:

(?<=\.?)([\w\s]{0,300}keyword.*?)(?=\.)

explain: 说明:

(?= subexpression) Zero-width positive lookahead assertion. (?= subexpression)零宽度正向前瞻断言。

(?<= subexpression) Zero-width positive lookbehind assertion. (?<= subexpression)零宽度正向后观断言。

*? Matches the previous element zero or more times, but as few times as possible. 匹配前一个元素零次或多次,但尽可能少。

and a simple code: 和一个简单的代码:

foreach (Match match in Regex.Matches(input, 
                                      @"(?<=\.?)([\w\s]{0,300}print.*?)(?=\.)"))
{
    Console.WriteLine(match.Groups[1].Value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM