Regualr Expression跳过空白 [英] Regualr Expression to skip white space
问题描述
我正在使用正则表达式从字符串中检索数据.
我有一种情况,逗号分隔符后可能有空格,也可能没有空格
我该如何处理.
例如:
"myName,age"在年龄之前存在一个空格,但是有可能我可以得到没有该空格的相同字符串,例如"myName,age".如何使用相同的正则表达式处理这两种情况.
请提前告知谢谢
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
推荐答案
如果您只想将它们分开,为什么要使用正则表达式而不使用split函数:
If you want to just separate them why using regular expressions , and not using split function :
string s = "myName , age ";
List<string> fields = s.Split(',').ToList();
for (int i = 0; i < fields.Count; i++)
{
fields[i] = fields[i].Trim();
}
您可以将带有"\s*[,]\s*
"的逗号识别为RE模式,但是我认为Split
函数可以解决您的问题.
希望对您有所帮助.
You can recognize commas with "\s*[,]\s*
" as the RE pattern but I think that Split
function will solve your problem.
Hope it helps.
为您的方案尝试以下RegEx代码:
Try the below RegEx code for your scenario:
Regex regexObj = new Regex(@"""[^""\r\n]*""|'[^'\r\n]*'|[^,\r\n]*");
请参阅以下内容:
http://stackoverflow.com/questions/4590298/how-to-ignore-whitespace-in-a-regular-expression-subject-string [ http://affy.blogspot.com/p5be/ch10.htm [
Refer these:
http://stackoverflow.com/questions/4590298/how-to-ignore-whitespace-in-a-regular-expression-subject-string[^]
http://affy.blogspot.com/p5be/ch10.htm[^]
这篇关于Regualr Expression跳过空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!