本文介绍了什么是正则表达式在.NET命名捕获组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我挣扎了正则表达式模式,将拉出从文本字符串到命名组。
A(有些武断)的例子最能说明什么,我想要的目的。
字符串输入=
玛丽·安妮一直在949宾果游戏比赛的最后于星期二24/04/2012她与数赢:4,6,11,16,19,27,45;
字符串模式=
@(LT;人> \ W +)一直到(小于?NumberOfGames>?\ d +)宾果游戏比赛的最后的。(LT;节> ...?)(<日期取代。 ?..),她赢得的数字:?(小于号> ...)?
正则表达式的regex =新的正则表达式(模式);
VAR匹配= regex.Match(输入);
串人= match.Groups [人]值。
字符串noOfGames = match.Groups [NumberOfGames]值。
字符串日= match.Groups [日]值。
字符串日= match.Groups [日期]值。
一串数字= match.Groups [编号]值。
我似乎无法获得正则表达式的工作,但我认为上述解释的不够好。基本上我需要得到人的名字,游戏的数量等。
任何人都可以解决这个问题,并解释他们的工作出实际的正则表达式?
解决方案
字符串模式= @(LT;人> [\ w] +)一直到(小于?; NumberOfGames> \ D +)赌博游戏\最后是在(小于?日> \ w +)(小于日期和GT; \ D \ D / \ D \ D / \ D {4})\她的赢数字:(?<数字> *?)$;
其他的帖子都提到了如何拔出组,但此正则表达式您输入相匹配。
I'm struggling with a regex pattern that will pull out text from a string into named groups.
A (somewhat arbitrary) example will best explain what I'm trying to achieve.
string input =
"Mary Anne has been to 949 bingo games. The last was on Tue 24/04/2012. She won with the Numbers: 4, 6, 11, 16, 19, 27, 45";
string pattern =
@"(?<Person>\w+?) has been to (?<NumberOfGames>\d+?) bingo games. The last was on (?<Day>...?) (?<Date>...?). She won with the Numbers: (?<Numbers>...?)";
Regex regex = new Regex(pattern);
var match = regex.Match(input);
string person = match.Groups["Person"].Value;
string noOfGames = match.Groups["NumberOfGames"].Value;
string day = match.Groups["Day"].Value;
string date = match.Groups["Date"].Value;
string numbers = match.Groups["Numbers"].Value;
I can't seem to get the regex pattern to work, but i think the above explains it well enough. Essentially i need to get the person name, the number of games etc.
Can anyone solve this and explain the actual regex pattern they worked out?
解决方案
string pattern = @"(?<Person>[\w ]+) has been to (?<NumberOfGames>\d+) bingo games\. The last was on (?<Day>\w+) (?<Date>\d\d/\d\d/\d{4})\. She won with the Numbers: (?<Numbers>.*?)$";
Other posts have mentioned how to pull out the groups, but this regex matches on your input.
这篇关于什么是正则表达式在.NET命名捕获组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!