Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curly brackets c# split issue

Tags:

c#

I have data in a string, email and id, in the format as shown below:

string s = "{email:[email protected]}{id:AB12345}";

I just want to remove curly brackets and extract the email and id from the above string in a variable as below.

string email = "[email protected]";
string id = "AB12345";

I have tried string.Format and other formatting. Since email and id size can vary, I can't find a solution.

like image 218
Ramesh Narayan Avatar asked Dec 12 '22 04:12

Ramesh Narayan


1 Answers

Use can use Regex.

var match = Regex.Match("{email:[email protected]}{id:AB12345}", @"\{email:(.+)\}\{id:(.+)\}");
var email = match.Groups[1].Value;
var id = match.Groups[2].Value;

PS: The pattern (.+) means that Email and the Id at least have 1 character, otherwise the match will not find the email and id, if those can be empty you can change it into (.*)

like image 127
Yuliam Chandra Avatar answered Dec 14 '22 16:12

Yuliam Chandra