Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.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_String_Split - Fatal编程技术网

C# 匹配词索引

C# 匹配词索引,c#,regex,string,split,C#,Regex,String,Split,我必须知道比赛是用哪个词进行的 我认为可以使用下面的代码,但是给我索引,里面没有单词计数器 是否可以在匹配发生的单词之后获取信息 const string stringToTest = "Am Rusch"; const string patternToMatch = @"\bRusch*"; Regex regex = new Regex(patternToMatch, RegexOptions.Compiled); MatchCollection matches = regex.Matc

我必须知道比赛是用哪个词进行的

我认为可以使用下面的代码,但是给我索引,里面没有单词计数器

是否可以在匹配发生的单词之后获取信息

const string stringToTest = "Am Rusch";
const string patternToMatch = @"\bRusch*";

Regex regex = new Regex(patternToMatch, RegexOptions.Compiled);

MatchCollection matches = regex.Matches(stringToTest);

foreach (Match match in matches)
{
    Console.WriteLine(match.Index);
}

字数应为1,因为在第二个单词中找到了匹配项。

按空格拆分
字符串stringToTest
,然后您可以轻松找到匹配发生在哪个单词中

按空格拆分
字符串stringToTest
,然后,您可以轻松找到匹配发生在哪个单词中

您可以玩一个小把戏来获取单词索引,在获取所需字符串的索引后,运行另一个正则表达式来获取其单词索引

const string stringToTest = "Am Rusch, you dare";
const string patternToMatch = @"\bRusch*";

Regex regex = new Regex(patternToMatch, RegexOptions.Compiled);

MatchCollection matches = regex.Matches(stringToTest);

foreach (Match match in matches)
{
    var wordIndex = Regex.Split(stringToTest.Substring(0, match.Index), "\\W").Count()-1;
    Console.WriteLine("Word Index: " + wordIndex);
}

这将返回字符串
单词索引:1

您可以玩一些小把戏来获取单词索引,在获取所需字符串的索引后,运行另一个正则表达式来获取其单词索引

const string stringToTest = "Am Rusch, you dare";
const string patternToMatch = @"\bRusch*";

Regex regex = new Regex(patternToMatch, RegexOptions.Compiled);

MatchCollection matches = regex.Matches(stringToTest);

foreach (Match match in matches)
{
    var wordIndex = Regex.Split(stringToTest.Substring(0, match.Index), "\\W").Count()-1;
    Console.WriteLine("Word Index: " + wordIndex);
}

这将返回字符串
单词索引:1

Regex是一种模式匹配工具,它不是为解释文本而设计的

如果想要一个基本的单词计数,使用找到的正则表达式索引,并将其传递给一个具有单词启发式的方法;例如这个扩展:

public static int AtWord(this string strBuffer, int index)
{ 
    int foundAt = -1;

    var splits = Regex.Split(strBuffer.Substring(0, index), @"(\s+)");

    if (splits.Any())
       foundAt = splits.Count() - 2;

    return foundAt;
}
用作

const string stringToTest = "Am Rusch Lunch";
const string patternToMatch = @"Rusch";

var match = Regex.Match(stringToTest, patternToMatch);

var wordIndex = stringToTest.AtWord(match.Index); // Returns 1, for a zero based list

Regex是一种模式匹配工具,其设计目的不是解释文本

如果想要一个基本的单词计数,使用找到的正则表达式索引,并将其传递给一个具有单词启发式的方法;例如这个扩展:

public static int AtWord(this string strBuffer, int index)
{ 
    int foundAt = -1;

    var splits = Regex.Split(strBuffer.Substring(0, index), @"(\s+)");

    if (splits.Any())
       foundAt = splits.Count() - 2;

    return foundAt;
}
用作

const string stringToTest = "Am Rusch Lunch";
const string patternToMatch = @"Rusch";

var match = Regex.Match(stringToTest, patternToMatch);

var wordIndex = stringToTest.AtWord(match.Index); // Returns 1, for a zero based list

最快的方法是将字符串拆分为单词,并找到与模式匹配的单词索引:

const string stringToTest = "Am Rusch";
const string patternToMatch = @"\bRusch*";
Console.WriteLine(Regex.Split(stringToTest,@"[^\w\p{M}]+")
        .Where(m => !string.IsNullOrEmpty(m))
        .ToList()
        .FindIndex(p => Regex.IsMatch(p,patternToMatch))
);
// Output: 1

说明:

  • Regex.Split(stringToTest,@“[^\w\p{M}]+”
    将字符串拆分为单词,因为
    [^\w\p{M}]+
    匹配单词和变音符号以外的一个或多个符号
  • .Where(m=>!string.IsNullOrEmpty(m))
    删除所有空元素
  • .FindIndex(p=>Regex.IsMatch(p,patternToMatch))
    获取所需单词的索引
A为了不删除空元素:

Regex.Matches(stringToTest,@"[\w\p{M}]+") // Match all words
        .Cast<Match>()                    // Cast to Matches array
        .Select(m => m.Value)             // Collect values only
        .ToList()                         // Convert to list
        .FindIndex(p => Regex.IsMatch(p, patternToMatch)) 
Regex.Matches(stringToTest,@“[\w\p{M}]+”/)匹配所有单词
.Cast()//转换为匹配数组
.Select(m=>m.Value)//仅收集值
.ToList()//转换为列表
.FindIndex(p=>Regex.IsMatch(p,patternToMatch))

最快的方法是将字符串拆分为单词,并找到与模式匹配的单词索引:

const string stringToTest = "Am Rusch";
const string patternToMatch = @"\bRusch*";
Console.WriteLine(Regex.Split(stringToTest,@"[^\w\p{M}]+")
        .Where(m => !string.IsNullOrEmpty(m))
        .ToList()
        .FindIndex(p => Regex.IsMatch(p,patternToMatch))
);
// Output: 1

说明:

  • Regex.Split(stringToTest,@“[^\w\p{M}]+”
    将字符串拆分为单词,因为
    [^\w\p{M}]+
    匹配单词和变音符号以外的一个或多个符号
  • .Where(m=>!string.IsNullOrEmpty(m))
    删除所有空元素
  • .FindIndex(p=>Regex.IsMatch(p,patternToMatch))
    获取所需单词的索引
A为了不删除空元素:

Regex.Matches(stringToTest,@"[\w\p{M}]+") // Match all words
        .Cast<Match>()                    // Cast to Matches array
        .Select(m => m.Value)             // Collect values only
        .ToList()                         // Convert to list
        .FindIndex(p => Regex.IsMatch(p, patternToMatch)) 
Regex.Matches(stringToTest,@“[\w\p{M}]+”/)匹配所有单词
.Cast()//转换为匹配数组
.Select(m=>m.Value)//仅收集值
.ToList()//转换为列表
.FindIndex(p=>Regex.IsMatch(p,patternToMatch))


在每个单词上使用正则表达式有什么问题?什么是单词<代码>这个句子有多少个单词?8否。正则表达式对前面的单词一无所知,因为它们与表达式不匹配。如果您需要知道哪个单词,为什么不能完全避免使用正则表达式,使用
String.Split
,然后遍历数组?在普通代码中,我使用正则表达式模式,因为我也必须找到不同的替代方法,因此正则表达式非常好。我结合了正则表达式和字符串拆分。谢谢你我没有想到的分割。在每个单词上使用正则表达式有什么问题?什么是单词<代码>这个句子有多少个单词?8否。正则表达式对前面的单词一无所知,因为它们与表达式不匹配。如果您需要知道哪个单词,为什么不能完全避免使用正则表达式,使用
String.Split
,然后遍历数组?在普通代码中,我使用正则表达式模式,因为我也必须找到不同的替代方法,因此正则表达式非常好。我结合了正则表达式和字符串拆分。谢谢你我没有想到的分裂。这和你说的使用.NET完成他的任务是一样的。请回答得更具体些。这和你说的使用.NET完成他的任务是一样的。请更具体地回答。我使用了string.split,但您的解决方案看起来更专业,可读性更好。我使用了string.split,但是你的解决方案看起来更专业,可读性更好。好的linq解决方案。它直截了当,分两步完成任务:1)获取所有单词2)找到第一个与模式匹配的单词。我就是这样理解这个问题的。它也很简洁。如果你愿意的话,你可以像OmegaMan的答案那样将其包装成一个扩展方法。很好的linq解决方案。它是直接的,分两步处理任务:1)获取所有单词2)找到第一个匹配模式的单词。我就是这样理解这个问题的。它也很简洁,如果你愿意的话,你可以像OmegaMan的答案那样把它包装成一个扩展方法。