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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12108582/
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-09 22:05:39  来源:igfitidea点击:

Extracting string between two characters?

c#.netstringvisual-studio-2010

提问by mr_eclair

I want to extract email id between < >

我想在两者之间提取电子邮件 ID < >

for example.

例如。

input string : "abc" <[email protected]>; "pqr" <[email protected]>;

输入字符串: "abc" <[email protected]>; "pqr" <[email protected]>;

output string : [email protected];[email protected]

输出字符串: [email protected];[email protected]

采纳答案by L.B

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

回答by O. R. Mapper

Use the String.IndexOf(char, int)methodto search for <starting at a given index in the string (e.g. the last index that you found a >character at, i.e. at the end of the previous e-mail address - or 0when 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)methodto extract the e-mail address whose start and end position is then known to you.

编写一个循环,只要找到另一个<字符就一直重复,每次找到一个<字符时,就查找下一个>字符。使用该String.Substring(int, int)方法提取您知道开始和结束位置的电子邮件地址。

回答by Pankaj Agarwal

string str = "\"abc\" <[email protected]>; \"pqr\" <[email protected]>;";
        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();
        }

回答by Alex

Could use the following regex and some linq.

可以使用以下正则表达式和一些 linq。

        var regex = new Regex(@"\<(.*?)\>");
        var input= @"""abc"" <[email protected]>;  ""pqr""  <[email protected]>";
        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 RegexI guess.

<> 括号之后被删除,你也可以将它整合到Regex我猜中。

回答by paparazzo

Tested

已测试

string input = "\"abc\" <[email protected]>; \"pqr\" <[email protected]>;";
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. I just did not realize it was correct until I wrote an answer myself.

LB+1的回答也是正确的。直到我自己写了一个答案,我才意识到这是正确的。

回答by Franco Dipré

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\" <[email protected]>;", '<', '>')

GetStringBetweenCharacters("\"abc\" <[email protected]>;", '<', '>')

you will get

你会得到

[email protected]

[email protected]