Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正则表达式,用于查找以空格开头并以空格结尾的所有单词_C#_Regex_Search_Whitespace - Fatal编程技术网

C# 正则表达式,用于查找以空格开头并以空格结尾的所有单词

C# 正则表达式,用于查找以空格开头并以空格结尾的所有单词,c#,regex,search,whitespace,C#,Regex,Search,Whitespace,我需要在字符串中查找以空格开头和结尾的单词。我在搜索空白时发现问题。然而,我可以做到以下几点。以@开头和结尾。任何关于空白的帮助都会很好 string input = "@@12@@ @@13@@"; foreach (Match match in Regex.Matches(input, @"@@\b\S+?\b@@")) { messagebox.show(match.Groups[1].Value); } 从文件: //为重

我需要在字符串中查找以空格开头和结尾的单词。我在搜索空白时发现问题。然而,我可以做到以下几点。以
@
开头和结尾。任何关于空白的帮助都会很好

string input = "@@12@@ @@13@@";             
foreach (Match match in Regex.Matches(input, @"@@\b\S+?\b@@"))                
{
   messagebox.show(match.Groups[1].Value);
}
从文件:

//为重复的单词定义正则表达式。
正则表达式rx=新正则表达式(@“\b(?\w+)\s+(\k)\b”,
RegexOptions.Compiled | RegexOptions.IgnoreCase);

在我看来,最好使用
string.Split()
而不是
Regex

var wordsArray = s.Split(new []{' '},StringSplitOptions.RemoveEmptyEntries);

如果您可以使用标准字符串方法轻松获得相同的结果,那么最好避免使用正则表达式。

\s+(?=我无法确切了解您的想法,但我希望此代码可以帮助您:

\s+(?=</)

string[]ha=input.Split(新[]{'@},StringSplitOptions.RemoveEmptyEntries);

我不想删除空格。我想搜索带有起始和结尾空格的单词。我得到了无法识别的转义序列。如果我使用@“\s+(?=我有这个”(\\$\\w+)“它查找以$开头、以空格结尾的单词。但我需要以空格开头和结尾的单词。
\s+(?=</)