Removing unwanted text outside <>

前端 未结 2 1018
忘了有多久
忘了有多久 2021-01-29 06:48

I want to remove all text except the text within <>\'s from a textbox.

相关标签:
2条回答
  • 2021-01-29 07:14

    This is off the top of my head, but hopefully will steer you in the right direction :)

    String email = "www.abc.com <abc@gmail.com>";
    String result = "";
    
    int firstIndex = email.IndexOf('<'); 
    int lastIndex = email.IndexOf('>');
    if(lastIndex > firstIndex)
        result = email.Substring(firstIndex + 1, lastIndex-firstIndex-1);
    
    0 讨论(0)
  • 2021-01-29 07:18

    Try this

    var strText = "asdasd<data1>sdsdf <data2>sdfsfsdf";
    var pattern = new Regex(@"\<(?<data>(.+?))\>");
    var matches = pattern.Matches(strText);
    foreach (Match match in matches)
    {
        Console.WriteLine("Data: " + match.Groups["data"]);
    } 
    //Output:
    //Data: data1
    //Data: data2
    
    0 讨论(0)
提交回复
热议问题