Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search string for substring

Tags:

c#

regex

I need to search a large string for a particular substring. The substring will start with Testing= but everything within the double quotes could be different because its a user login.

So examples of the substring I need are

Testing="network\smithj"  

or

Testing="network\rodgersm"  

Do my requirements make sense? How can I do this in C#?

like image 485
Josh Avatar asked Dec 01 '22 05:12

Josh


1 Answers

This is a great application of a regular expression.

"Testing=\"[^\"]*\""

You will use it like so:

Regex reg = new Regex("Testing=\"[^\"]*\"");
string login = reg.Match(yourInputString).Groups[0].Value;

The above works with your two given test cases.

Wikipedia has a great article on Regular Expressions if you are not familiar with them. And if you search google you can find a wealth of info on how to use Regular Expressions in C#.

like image 137
jjnguy Avatar answered Dec 05 '22 07:12

jjnguy