本文介绍了Regualr Expression跳过空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用正则表达式从字符串中检索数据.
我有一种情况,逗号分隔符后可能有空格,也可能没有空格
我该如何处理.

例如:

"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

推荐答案

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 regexObj = new Regex(@"""[^""\r\n]*""|'[^'\r\n]*'|[^,\r\n]*");



这篇关于Regualr Expression跳过空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 09:49