ghoston3rd
ghoston3rd

Reputation: 149

Regex to remove text between two chars in c#

I have the following string that I will need to remove everything between =select and the following } char

ex. Enter Type:=select top 10 type from cable}

The end result is the string variable to just show Enter Type:

I was looking for a way to do this with Regex, but I'm open to other methods as well. Thanks in advance for the help.

Upvotes: 0

Views: 582

Answers (2)

Jerren Saunders
Jerren Saunders

Reputation: 1435

string input = "Enter Type:=select top 10 type from cable}";

System.Text.RegularExpressions.Regex regExPattern = new System.Text.RegularExpressions.Regex("(.*):=select.*}");
System.Text.RegularExpressions.Match match = regExPattern.Match(input);

string output = String.Empty;
if( match.Success)
{
    output = match.Groups[1].Value;
}

Console.WriteLine("Output = " + output);

The value of the 'output' variable will be the value found before the ":=select" segment of the input string. If you need to pull out additional information from the input string, surround it will parenthesis and matches found will be added to the match.Groups array. By the way, the value of match.Groups[0].Value is the original string.

Upvotes: 1

ig-melnyk
ig-melnyk

Reputation: 2879

var rx = new Regex("=select[^}]*}");;

Console.WriteLine(rx.Replace ("Enter Type:=select top 10 type from cable}", ""));

Regexp.Replace(string input,string output) function replaces all substrings that match given regexp with string "output". First line defines regexp that matches everything between =select and }

Upvotes: 0

Related Questions