Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting string between two characters?

I want to extract email id between < >

for example.

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

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

like image 921
Vishwanath Dalvi Avatar asked Aug 24 '12 11:08

Vishwanath Dalvi


2 Answers

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]>;", '<', '>')

you will get

[email protected]

like image 64
Franco Dipre Avatar answered Oct 27 '22 09:10

Franco Dipre


string input = @"""abc"" <[email protected]>; ""pqr"" <[email protected]>;";
var output = String.Join(";", Regex.Matches(input, @"\<(.+?)\>")
                                    .Cast<Match>()
                                    .Select(m => m.Groups[1].Value));
like image 41
L.B Avatar answered Oct 27 '22 10:10

L.B