简体   繁体   English

如何在C#中使用Regex匹配字符串

[英]How to match string with Regex in C#

I have this string Sample Text <test@test.com> and this string test@test.com and I'm trying to match the preceeding text ("Sample Text" in this example) if it exists and the email without the "<",">" characters. 我有此字符串Sample Text <test@test.com>和此字符串test@test.com并且我尝试匹配前面的文本(在此示例中为“ Sample Text”)(如果存在)以及不包含“ < “,”>“个字符。 There may be whitespaces at before and after that. 在此之前和之后可能会有空白。 At first I used Regex.Split with this expression @"\\s*(.*)<(.*@.*)>\\s*" but it gave me 4 strings instead of 2. The 2 strings that I wanted were correct but it also returned empty strings. 最初,我使用Regex.Split并使用此表达式@"\\s*(.*)<(.*@.*)>\\s*"但它给了我4个字符串而不是2个字符串。我想要的2个字符串是正确的但它也返回空字符串。 Now I'm trying with Regex.Matches using this expression @"\\s*(.*)(?: <)?(.*@.*)(?:>)?\\s*" it finds 3 matches. 现在我正在尝试使用Regex.Matches,使用此表达式@"\\s*(.*)(?: <)?(.*@.*)(?:>)?\\s*"找到3个匹配项。 The 2 are again the correct ones and the other is the input string itself. 2还是正确的,另一个是输入字符串本身。 As for the second string it doesn't work. 至于第二个字符串则不起作用。 How do I fix this? 我该如何解决?

This could be done without regex. 这可以不用正则表达式来完成。 Take a look onto MailAddress class; 看一下MailAddress类; it could be used to parse strings like in your example: 它可以用来解析字符串,例如您的示例:

var mailAddress = new MailAddress("Sample Text <test@test.com>");

Here mailAddress.Address property will contain test@test.com value, and mailAddress.DisplayName will contain Sample Text value. 在这里, mailAddress.Address属性将包含test@test.com值,而mailAddress.DisplayName将包含Sample Text值。

Based on your test cases this regex may work.. 根据您的测试用例,此正则表达式可能有效。

(.*)\s?\<(.*)\>

This will give you to results 1 the preceding text & 2 the text contained within the <> brackets 这将使您得到结果1前面的文本和2包含在<>括号内的文本

If you care about ensuring the email is valid you may wish to look at a more thorough email regex, but I am guess you are trying to match a string that has come from an email or mail server so that may not be a problem. 如果您关心确保电子邮件有效,则不妨查看更全面的电子邮件正则表达式,但是我想您正在尝试匹配来自电子邮件或邮件服务器的字符串,因此可能不会出现问题。

Also, its worth grabbing a regex building program such as Expresso or using one of the many online tools to help build your regex. 此外,值得抓住诸如Expresso之类的正则表达式构建程序,或使用许多在线工具之一来帮助构建正则表达式。

Regex.Matches always return the full match on the first match, so just ignore it and use the second and third. Regex.Matches总是在第一个匹配项上返回完整匹配项,因此只需忽略它并使用第二个和第三个匹配项。

To match the second type of string (only email) you better match the first type and if not found match the second using a single email regex 要匹配第二种类型的字符串(仅电子邮件),您最好匹配第一种类型,如果找不到,则使用单个电子邮件正则表达式匹配第二种类型

Try this one here 在这里尝试这个

\s*(.*?)(?: <)?(\S*@.*)(?:>)?\s*

I changed yours only a bit. 我只改变了你一点。

  1. added into the first group the ? 加入第一组? to make it a lazy match 使它变得懒惰

  2. changed the part before the @ into \\S , what means anything but whitespace. @之前的部分更改为\\S ,除了空格以外什么都没有。

You can see it online here on Rubular 您可以在Rubular上在线查看

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

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