获得Regex匹配的整条线

时间:2021-11-04 19:26:39

I have some multiline text and I want to find the lines that contain a specific word.

我有一些多行文字,我想找到包含特定单词的行。

In the current implementation I only get the word, but instead I would like to get the whole line. Here's the code:

在目前的实现中,我只得到了这个词,但我希望得到整行。这是代码:

var finder = new Regex(@"(^|\W)" + Regex.Escape(wordToFind) + @"(\W|$)", RegexOptions.IgnoreCase);
 foreach (var match in finder.Matches(multilineString))
 {
      //match should be the whole line
 }

Example:

If Request.QueryString("bar") <> "" Then
    Set bar= foo("baz")
Else
    Set bar= foo("baz2")
End If

If I look for foo I should get:

如果我寻找foo,我应该得到:

Set bar= foo("baz")
Set bar= foo("baz2")

I didn't implement the regex and I'm not very familiar with Regular Expressions, I would appreciate if someone could give me some hints to keep investigating.

我没有实现正则表达式,我对正则表达式不是很熟悉,如果有人能给我一些提示继续进行调查,我将不胜感激。

Thanks

2 个解决方案

#1


1  

You can try with this regex:

你可以试试这个正则表达式:

Regex regex = new Regex(@"^.*?\W" + Regex.Escape(wordToFind) + @"\W.*?$");

The ^ matches the start of the string or line, the $ at the end matches the end of string or line.
The .*? matches everything (but as little as possible), and \W (uppercase "W") matches any non-word character (characters that are neither a letter nor a digit).

^匹配字符串或行的开头,结尾的$匹配字符串或行的结尾。 。*?匹配所有内容(但尽可能少),\ W(大写“W”)匹配任何非单词字符(既不是字母也不是数字的字符)。

Alternatively you can use \s (lowercase "s") instead of \W if you want your words to be separated by whitespaces only.

或者,如果您希望单词仅用空格分隔,则可以使用\ s(小写“s”)代替\ W.

Here is a good reference for Regex.

这是Regex的一个很好的参考。

#2


0  

You can do it like this

你可以这样做

string[] lines = multilinestring.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
List<string> validString = new List<string>();
foreach(string s in lines)
{
   if(finder.Match(s).Success)
   {
      validString.Add(s);
   }
}

give this a try as well, should work

试一试,应该工作

List<string> lines = multilinestring.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).ToList();
List<string> validString =  lines.Where(x => finder.IsMatch(x)).ToList();

#1


1  

You can try with this regex:

你可以试试这个正则表达式:

Regex regex = new Regex(@"^.*?\W" + Regex.Escape(wordToFind) + @"\W.*?$");

The ^ matches the start of the string or line, the $ at the end matches the end of string or line.
The .*? matches everything (but as little as possible), and \W (uppercase "W") matches any non-word character (characters that are neither a letter nor a digit).

^匹配字符串或行的开头,结尾的$匹配字符串或行的结尾。 。*?匹配所有内容(但尽可能少),\ W(大写“W”)匹配任何非单词字符(既不是字母也不是数字的字符)。

Alternatively you can use \s (lowercase "s") instead of \W if you want your words to be separated by whitespaces only.

或者,如果您希望单词仅用空格分隔,则可以使用\ s(小写“s”)代替\ W.

Here is a good reference for Regex.

这是Regex的一个很好的参考。

#2


0  

You can do it like this

你可以这样做

string[] lines = multilinestring.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
List<string> validString = new List<string>();
foreach(string s in lines)
{
   if(finder.Match(s).Success)
   {
      validString.Add(s);
   }
}

give this a try as well, should work

试一试,应该工作

List<string> lines = multilinestring.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).ToList();
List<string> validString =  lines.Where(x => finder.IsMatch(x)).ToList();