简体   繁体   English

提取两个字符之间的字符串?

[英]Extracting string between two characters?

I want to extract email id between < > 我想在< >之间提取电子邮件ID

for example. 例如。

input string : "abc" <abc@gmail.com>; "pqr" <pqr@gmail.com>; 输入字符串: "abc" <abc@gmail.com>; "pqr" <pqr@gmail.com>; "abc" <abc@gmail.com>; "pqr" <pqr@gmail.com>;

output string : abc@gmail.com;pqr@gmail.com 输出字符串: abc@gmail.com;pqr@gmail.com

string input = @"""abc"" <abc@gmail.com>; ""pqr"" <pqr@gmail.com>;";
var output = String.Join(";", Regex.Matches(input, @"\<(.+?)\>")
                                    .Cast<Match>()
                                    .Select(m => m.Groups[1].Value));

Tested 经测试

string input = "\"abc\" <abc@gmail.com>; \"pqr\" <pqr@gmail.com>;";
matchedValuesConcatenated = string.Join(";", 
                                Regex.Matches(input, @"(?<=<)([^>]+)(?=>)")
                                .Cast<Match>()
                                .Select(m => m.Value));

(?<=<) is a non capturing look behind so < is part of the search but not included in the output (?<= <)是一个非捕获的背后,所以<是搜索的一部分,但不包括在输出中

The capturing group is anything not > one or more times 捕获组不是>一次或多次

Can also use non capturing groups @"(?:<)([^>]+)(?:>)" 也可以使用非捕获组@“(?:<)([^>] +)(?:>)”

The answer from LB +1 is also correct. LB +1的答案也是正确的。 I just did not realize it was correct until I wrote an answer myself. 直到我自己写了一个答案,我才意识到它是正确的。

Without regex, you can use this: 没有正则表达式,您可以使用:

public static string GetStringBetweenCharacters(string input, char charFrom, char charTo)
    {
        int posFrom = input.IndexOf(charFrom);
        if (posFrom != -1) //if found char
        {
            int posTo = input.IndexOf(charTo, posFrom + 1);
            if (posTo != -1) //if found char
            {
                return input.Substring(posFrom + 1, posTo - posFrom - 1);
            }
        }

        return string.Empty;
    }

And then: 接着:

GetStringBetweenCharacters("\\"abc\\" <abc@gmail.com>;", '<', '>')

you will get 你会得到

abc@gmail.com

Use the String.IndexOf(char, int) method to search for < starting at a given index in the string (eg the last index that you found a > character at, ie at the end of the previous e-mail address - or 0 when looking for the first address). 使用String.IndexOf(char, int)方法搜索<从字符串中的给定索引开始(例如,您找到>字符的最后一个索引,即在上一个电子邮件地址的末尾 - 或0在寻找第一个地址时)。

Write a loop that repeats for as long as you find another < character, and everytime you find a < character, look for the next > character. 只要找到另一个<字符,就写一个重复的循环,每次找到<字符时,查找下一个>字符。 Use the String.Substring(int, int) method to extract the e-mail address whose start and end position is then known to you. 使用String.Substring(int, int)方法提取当前已知其开始和结束位置的电子邮件地址。

Could use the following regex and some linq. 可以使用以下正则表达式和一些linq。

        var regex = new Regex(@"\<(.*?)\>");
        var input= @"""abc"" <abc@gmail.com>;  ""pqr""  <pqr@gmail.com>";
        var matches = regex.Matches(input);
       var res = string.Join(";", matches.Cast<Match>().Select(x => x.Value.Replace("<","").Replace(">","")).ToArray());

The <> brackets get removed afterwards, you could also integrate it into Regex I guess. 之后<\\ n>括号被移除,我猜也可以将它集成到正则Regex

string str = "\"abc\" <abc@gmail.com>; \"pqr\" <pqr@gmail.com>;";
        string output = string.Empty;
        while (str != string.Empty)
        {
            output += str.Substring(str.IndexOf("<") + 1, str.IndexOf(">") -1);
            str = str.Substring(str.IndexOf(">") + 2, str.Length - str.IndexOf(">") - 2).Trim();
        }

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

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