Click here to Skip to main content
15,900,532 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using regular expression to retrieve data from a string.
i have a scenario where in there may be or may not be white spaces after comma seperator
how shall i handle this.

for eg:

"myName, age" here before age there is a white space but there are chances i can get the same string with out white spaces like this "myName,age" . How shall i handle both cases using same regular expression.
please advise thanks in advance
Posted

If you want to just separate them why using regular expressions , and not using split function :

C#
string s = "myName  ,   age  ";
List<string> fields = s.Split(',').ToList();
for (int i = 0; i < fields.Count; i++)
{
    fields[i] = fields[i].Trim();
}


You can recognize commas with "\s*[,]\s*" as the RE pattern but I think that Split function will solve your problem.

Hope it helps.
 
Share this answer
 
Try the below RegEx code for your scenario:

C#
Regex regexObj = new Regex(@"""[^""\r\n]*""|'[^'\r\n]*'|[^,\r\n]*");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900