使用C#regex,获取两个字符串之间的值[重复]

时间:2022-11-27 18:50:09

This question already has an answer here:

这个问题在这里已有答案:

My test data

我的测试数据

Date:Fri 14-Mar-2003 Venue:S.F.S. Crowd:24,172

The data I am interested in

我感兴趣的数据

Fri 14-Mar-2003

The code I currently have

我目前的代码

string datePattern = "Date:(.*?) Venue";
string tempDate = Regex.Match(values[(int)HomeColumnNames.VenueCrowdDate], datePattern).Value;

The data that is being returned

正在返回的数据

Date:Fri 14-Mar-2003 Venue

Any advice or assistance would be much appreciated.

任何建议或协助将不胜感激。

4 个解决方案

#1


1  

Your regex is ok. Just you need to get group one.

你的正则表达式还可以。只是你需要得到第一组。

var match= Regex.Match(values[(int)HomeColumnNames.VenueCrowdDate], datePattern);

match.Groups[0]; //returns full match
match.Groups[1]; //returns 1st group

//Gets MatchCollection
var matches= Regex.Matches(values[ManyAddresses, datePattern);

using @bash.d's pattern is better for other samples.

使用@ bash.d的模式对其他样本更好。

#2


1  

You can also loop thought the matched groups.

您还可以循环思考匹配的组。

var groups = Regex.Match(values[(int)HomeColumnNames.VenueCrowdDate], datePattern).Groups;
Console.WriteLine(groups[1].Value); //Fri 14-Mar-2003

#3


1  

You are including the words into your regex, so they will be in the extracted string. Try

您将这些单词包含在正则表达式中,因此它们将位于提取的字符串中。尝试

string datePattern = @"{\w+}\s+{\d{1,2}-{\w+}-{\d{4,}";

#4


0  

You're returning the match, not your capture group.

你将返回比赛,而不是你的捕获组。

The following code will allow you to explicitly name (and subsequently reference) your capture. It's not necessary to explicitly name or number your groups... But it's good practice.

以下代码将允许您明确命名(并随后引用)捕获。没有必要明确地命名或编号你的团队......但这是一个好习惯。

String groupName = "yourGroupName";
Regex r = new Regex(@"Date:(?<" + groupName + ">.*?) Venue");
Matches m = r.Match(yourTestData);
Console.WriteLine(m.Groups[groupName]);

#1


1  

Your regex is ok. Just you need to get group one.

你的正则表达式还可以。只是你需要得到第一组。

var match= Regex.Match(values[(int)HomeColumnNames.VenueCrowdDate], datePattern);

match.Groups[0]; //returns full match
match.Groups[1]; //returns 1st group

//Gets MatchCollection
var matches= Regex.Matches(values[ManyAddresses, datePattern);

using @bash.d's pattern is better for other samples.

使用@ bash.d的模式对其他样本更好。

#2


1  

You can also loop thought the matched groups.

您还可以循环思考匹配的组。

var groups = Regex.Match(values[(int)HomeColumnNames.VenueCrowdDate], datePattern).Groups;
Console.WriteLine(groups[1].Value); //Fri 14-Mar-2003

#3


1  

You are including the words into your regex, so they will be in the extracted string. Try

您将这些单词包含在正则表达式中,因此它们将位于提取的字符串中。尝试

string datePattern = @"{\w+}\s+{\d{1,2}-{\w+}-{\d{4,}";

#4


0  

You're returning the match, not your capture group.

你将返回比赛,而不是你的捕获组。

The following code will allow you to explicitly name (and subsequently reference) your capture. It's not necessary to explicitly name or number your groups... But it's good practice.

以下代码将允许您明确命名(并随后引用)捕获。没有必要明确地命名或编号你的团队......但这是一个好习惯。

String groupName = "yourGroupName";
Regex r = new Regex(@"Date:(?<" + groupName + ">.*?) Venue");
Matches m = r.Match(yourTestData);
Console.WriteLine(m.Groups[groupName]);