Extract string between two characters using Regular Expression in ASP.Net

Last Reply 26 days ago By dharmendr

Posted 27 days ago

Drink: Fanta Strawberry + 0.00 USD <br/> Side: Classic chips + 0.00 USD <br/> Option 1: Asian Ginger Chicken Egg Roll + 0.00 USD <br/> Option 2: Chipotle Chicken Egg Roll + 0.00 USD <br/> Option 3: V Greens and Cornbread Egg Roll + 0.00 USD <br/>

I have this string which need to break down, i want to get all the values between semicolon and plus sign from above string

: <get all the values between these> +

i need to break it and return should be like this

Fanta Strawberry Classic chips Asian Ginger Chicken Egg Roll Chipotle Chicken Egg Roll V Greens and Cornbread Egg Roll

You are viewing reply posted by: dharmendr 26 days ago.
Posted 26 days ago

Hi nauna,

Check this example. Now please take its reference and correct your code.

Namespaces

C#

using System.Linq;
using System.Text.RegularExpressions;

VB.Net

Imports System.Linq
Imports System.Text.RegularExpressions

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    string input = "Drink: Fanta Strawberry + 0.00 USD <br/> Side: Classic chips + 0.00 USD <br/> ";
    input += "Option 1: Asian Ginger Chicken Egg Roll + 0.00 USD <br/> ";
    input += "Option 2: Chipotle Chicken Egg Roll + 0.00 USD <br/> ";
    input += "Option 3: V Greens and Cornbread Egg Roll + 0.00 USD <br/>";
    string[] result = Regex.Matches(input, @"\:(.+?)\+").Cast<Match>().Select(s => s.Groups[1].Value.Trim()).ToArray();
    Response.Write(string.Join(" ", result));
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim input As String = "Drink: Fanta Strawberry + 0.00 USD <br/> Side: Classic chips + 0.00 USD <br/> "
    input += "Option 1: Asian Ginger Chicken Egg Roll + 0.00 USD <br/> "
    input += "Option 2: Chipotle Chicken Egg Roll + 0.00 USD <br/> "
    input += "Option 3: V Greens and Cornbread Egg Roll + 0.00 USD <br/>"
    Dim result As String() = Regex.Matches(input, "\:(.+?)\+").Cast(Of Match)().Select(Function(s) s.Groups(1).Value.Trim()).ToArray()
    Response.Write(String.Join(" ", result))
End Sub

Output

Fanta Strawberry Classic chips Asian Ginger Chicken Egg Roll Chipotle Chicken Egg Roll V Greens and Cornbread Egg Roll