Honghao
Honghao

Reputation: 21

C# Regular Expression find the string after a string

I have a connection string

"User ID=abc;Password=pwd;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=PRDREPTQ)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=PRRQ)))"

I want to use regular expression to parse out the host "PRDREPTQ" value only. Can anybody help me write the pattern?

Upvotes: 0

Views: 2300

Answers (2)

Amit Joki
Amit Joki

Reputation: 59232

Use this:

Regex HOST=(\w+)

And capture the first group

string testString = "User ID=abc;Password=pwd;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=PRDREPTQ)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=PRRQ)))";
var match= Regex.Match(testString, @"HOST=(\w+)");
Console.WriteLine(match.Groups[1]);

Upvotes: 0

Lloyd
Lloyd

Reputation: 29668

Easiest solution:

\(HOST=(.*?)\)

For example:

enter image description here

So in C#:

Match match = Regex.Match(inputString,@"\(HOST=(.*?)\)",RegexOptions.IgnoreCase | RegexOptions.Singleline);
string host_value = match.Groups[1].Value;

Upvotes: 2

Related Questions