简体   繁体   English

Regex.Match 没有给出预期的结果

[英]Regex.Match does not give expected result

I am using C# in Visual Studio 2017. I have below string我在 Visual Studio 2017 中使用 C#。我有以下字符串

inputvalue = "
    OrderId:009
    Email:Ardi1234@yahoo.com
    ProductId:X206"

The below code should taken email address下面的代码应该取email地址

var email =  Regex.Match(input: inputvalue, 
                     pattern: @"Email\:\ (.+)$", 
                     options: RegexOptions.Multiline).Groups[1].Value;

I want to get just email Address.我只想得到 email 地址。 but the result is an empty string.但结果是一个空字符串。 How can I do that?我怎样才能做到这一点?

Maybe, a bit over-engineered, yet it would extract the email address if there'd be any undesired spaces:也许,有点过度设计,但如果有任何不需要的空格,它会提取 email 地址:

(?is)\bEmail:\s*([^@]+@[^.]+\.[a-z0-9]{2,6}(?:\.[a-z0-9]{2,6})?)$

Test测试

using System;
using System.Text.RegularExpressions;

public class Test
{
    public static void Main()
    {
        string pattern = @"(?is)\bEmail:\s*([^@]+@[^.]+\.[a-z0-9]{2,6}(?:\.[a-z0-9]{2,6})?)$";
        string input = @"OrderId:009
            Email:Ardi1234@yahoo.com
            ProductId:X206

            OrderId:009
                Email: Ardi1234@yahoo.co.uk
                ProductId:X206

            OrderId:009
                EMAIL: Ardi1234@yahoo.co.uk
                ProductId:X206";
        RegexOptions options = RegexOptions.Multiline;

        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}

C# Demo C# 演示


If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com .如果您想简化/修改/探索表达式,它已在regex101.com的右上角面板上进行了解释。 If you'd like, you can also watch in this link , how it would match against some sample inputs.如果您愿意,您还可以在此链接中观看它如何与一些示例输入匹配。


RegEx Circuit正则表达式电路

jex.im visualizes regular expressions: jex.im可视化正则表达式:

在此处输入图像描述

You can try: Email\:(.+) not clear if that input string as newline breaks though.您可以尝试: Email\:(.+)不清楚是否输入字符串作为换行符换行符。

PS: pretty helpfull tool: https://www.regextester.com PS:非常有用的工具: https://www.regextester.com

The following seems to work:以下似乎有效:

var inputvalue = @"
OrderId:009
Email:Ardi1234@yahoo.com
ProductId:X206";

var email =  Regex.Match(input: inputvalue, 
                    pattern: @"Email:(.+)$", 
                    options: RegexOptions.Multiline).Groups[1].Value;
Console.WriteLine(email);
Ardi1234@yahoo.com

No regex没有正则表达式

Regular expressions are awesome, but there is a way here without using them.正则表达式很棒,但这里有一种不使用它们的方法。

var inputvalue = @"
OrderId:009
Email:Ardi1234@yahoo.com
ProductId:X206";

var email = inputvalue.Split()
    .FirstOrDefault(l => l.StartsWith("email:", ignoreCase: true, culture: CultureInfo.InvariantCulture))
    ?.Replace("email:", "",ignoreCase: true, CultureInfo.InvariantCulture);

Console.WriteLine(email);

Output: Output:

Ardi1234@yahoo.com

I've always preferred named capturing groups as they avoid filling code with magic numbers.我一直更喜欢命名捕获组,因为它们避免用幻数填充代码。 The problem with the code though is that you have an extra space in the regex compared to the data you're hoping to match.代码的问题在于,与您希望匹配的数据相比,您在正则表达式中有额外的空间。 There isn't any need to escape space, or colon here:这里不需要转义空格或冒号:

Regex.Match(input: inputvalue,
            pattern: @"Email:(?<eml>.+)$",
            options: RegexOptions.Multiline).Groups["eml"].Value;

I noted you have multiline set to true, so $ means "end of line" rather than "end of entire input" so the email address is correctly captured but it might well contain a trailing \r - watch out for this if it will be a problem我注意到您已将多行设置为 true,因此 $ 表示“行尾”而不是“整个输入的结束”,因此 email 地址被正确捕获,但它很可能包含尾随\r - 如果它会出现,请注意这一点一个问题

If your file will potentially have some spaces between the colon and the email address, consider this pattern:如果您的文件可能在冒号和 email 地址之间有一些空格,请考虑以下模式:

Regex.Match(input: inputvalue,
            pattern: @"Email: *(?<eml>.+)$",
            options: RegexOptions.Multiline).Groups["eml"].Value;

This will match "zero or more spaces" between the colon and the email but they won't be captured into the email group这将匹配冒号和 email 之间的“零个或多个空格”,但它们不会被捕获到 email 组中

With no spaces, regex can match spaces:没有空格,正则表达式可以匹配空格:

在此处输入图像描述

With spaces, regex can match spaces:使用空格,正则表达式可以匹配空格:

在此处输入图像描述

With no spaces, regex cannot match spaces:没有空格,正则表达式不能匹配空格:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM