C# 仅从正则表达式返回匹配的一部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/628556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:56:27  来源:igfitidea点击:

Returning only part of match from Regular Expression

c#regex

提问by

Say I have the string "User Name:firstname.surname" contained in a larger string how can I use a regular expression to just get the firstname.surname part?

假设我在一个更大的字符串中包含字符串“User Name:firstname.surname”,我如何使用正则表达式来获取 firstname.surname 部分?

Every method i have tried returns the string "User Name:firstname.surname" then I have to do a string replace on "User Name:" to an empty string.

我尝试过的每种方法都会返回字符串“用户名:名字.姓氏”,然后我必须将“用户名:”上的字符串替换为空字符串。

Could back references be of use here?

反向引用在这里有用吗?

Edit:

编辑:

The longer string could contain "Account Name: firstname.surname" hence why I want to match the "User Name:" part of the string aswell to just get that value.

较长的字符串可能包含“帐户名:名字.姓氏”,因此我想匹配字符串的“用户名:”部分以获取该值。

采纳答案by Daniel LeCheminant

I like to use named groups:

我喜欢使用命名组:

Match m = Regex.Match("User Name:first.sur", @"User Name:(?<name>\w+\.\w+)");
if(m.Success)
{
   string name = m.Groups["name"].Value;
}

Putting the ?<something>at the beginning of a group in parentheses (e.g. (?<something>...)) allows you to get the value from the match using somethingas a key (e.g. from m.Groups["something"].Value)

将 放在?<something>括号中的组开头(例如(?<something>...))允许您使用something作为键(例如 from m.Groups["something"].Value)从匹配中获取值

If you didn't want to go to the trouble of naming your groups, you could say

如果你不想麻烦地命名你的组,你可以说

Match m = Regex.Match("User Name:first.sur", @"User Name:(\w+\.\w+)");
if(m.Success)
{
   string name = m.Groups[1].Value;
}

and just get the first thing that matches. (Note that the first parenthesized group is at index 1; the whole expression that matches is at index 0)

并获得匹配的第一件事。(请注意,第一个带括号的组在 index 处1;匹配的整个表达式在 index 处0

回答by David Sykes

All regular expression libraries I have used allow you to define groups in the regular expression using parentheses, and then access that group from the result.

我使用过的所有正则表达式库都允许您使用括号在正则表达式中定义组,然后从结果中访问该组。

So, your regexp might look like: User name:([^.].[^.])

因此,您的正则表达式可能如下所示: 用户名:([^.].[^.])

The complete match is group 0. The part that matches inside the parentheses is group 1.

完全匹配为第 0 组。括号内匹配的部分为第 1 组。

回答by Tor Haugen

Make a group with parantheses, then get it from the Match.Groupscollection, like this:

用括号创建一个组,然后从Match.Groups集合中获取它,如下所示:

string s = "User Name:firstname.surname";
Regex re = new Regex(@"User Name:(.*\..*)");
Match match = re.Match(s);
if (match.Success)
{
    MessageBox.Show(match.Groups[1].Value);
}

(note: the first group, with index 0, is the whole match)

(注意:第一组,索引为 0,是整场比赛)

回答by Tor Haugen

You could also try the concept of "lookaround". This is a kind of zero-width assertion, meaning it will match characters but it won't capture them in the result.

您也可以尝试“环视”的概念。这是一种零宽度断言,这意味着它将匹配字符但不会在结果中捕获它们。

In your case, we could take a positive lookbehind: we want what's behind the target string "firstname.surname" to be equal to "User Name:".

在您的情况下,我们可以采取积极的回顾:我们希望目标字符串“firstname.surname”后面的内容等于“User Name:”。

Positive lookbehind operator: (?<=StringBehind)StringWeWant

正向后视操作符:(?<=StringBehind)StringWeWant

This can be achieved like this, for instance (a little Java example, using string replace):

这可以像这样实现,例如(一个小 Java 示例,使用字符串替换):

String test = "Account Name: firstname.surname; User Name:firstname.surname";
String regex = "(?<=User Name:)firstname.surname";
String replacement = "James.Bond";
System.out.println(test.replaceAll(regex, replacement));

This replaces only the "firstname.surname" strings that are preceeded by "User Name:" without replacing the "User Name:" itself - which is not returned by the regex, only matched.

这仅替换以“用户名:”开头的“firstname.surname”字符串,而不替换“用户名:”本身 - 正则表达式不返回,仅匹配。

OUTPUT: Account Name: firstname.surname; User Name:James.Bond

输出:账户名:firstname.surname;用户名:詹姆斯.邦德

That is, if the language you're using supports this kind of operations

也就是说,如果您使用的语言支持这种操作