如何在iPhone应用程序中使用正则表达式来分隔字符串,(逗号) [英] How to use regular expression in iPhone app to separate string by , (comma)

查看:154
本文介绍了如何在iPhone应用程序中使用正则表达式来分隔字符串,(逗号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须阅读有三列的.csv档案。在解析.csv文件时,我会获得此格式的字符串 Christopher Bass,\Cry the Beloved Country Final Essay \cbass @ cgs.k12.va.us 。我想把一个数组中的三列的值,所以我使用 componentSeparatedByString:@,方法!它成功返回了包含三个组件的数组:

I have to read .csv file which has three columns. While parsing the .csv file, I get the string in this format Christopher Bass,\"Cry the Beloved Country Final Essay\",cbass@cgs.k12.va.us. I want to store the values of three columns in an Array, so I used componentSeparatedByString:@"," method! It is successfully returning me the array with three components:


  1. Christopher Bass

  2. 最终论文

  3. cbass@cgs.k12.va.us

列值中的逗号,像这样
Christopher Bass,\Cry,Beloved Country Final Essay \,cbass @ cgs.k12.va.us
它分隔四个组件中的字符串,因为在Cry之后有一个(逗号):

but when there is already a comma in the column value, like this Christopher Bass,\"Cry, the Beloved Country Final Essay\",cbass@cgs.k12.va.us it separates the string in four components because there is a ,(comma) after the Cry:


  1. Christopher Bass

  2. Cry

  3. 受欢迎的国家/地区最终论文

  4. cbass@cgs.k12.va.us

  1. Christopher Bass
  2. Cry
  3. the Beloved Country Final Essay
  4. cbass@cgs.k12.va.us

所以,我该如何使用正则表达式来处理。我有RegexKitLite类,但我应该使用正则表达式。请帮助!

so, How can I handle this by using regular expression. I have "RegexKitLite" classes but which regular expression should I use. Please help!

感谢 -

推荐答案

p>任何正则表达式可能会出现同样的问题,你需要的是通过转义你的逗号或通过突出显示字符串的方式清除你的条目或字符串:My string。否则你会有同样的问题。祝你好运。

Any regular expression would probably turn out with the same problem, what you need is to sanitize your entries or strings, either by escaping your commas or by highlighting strings this way: "My string". Otherwise you will have the same problem. Good luck.

对于你的例子,你可能需要做如下:

For your example you would probably need to do something like:

\"Christopher Bass\",\"Cry\, the Beloved Country Final Essay\",\"cbass@cgs.k12.va.us\"

这样你可以使用一个regexp,甚至从 NSString class。

That way you could use a regexp or even the same method from the NSString class.

完全不相关,但清除字符串的重要性: http://xkcd.com/327/ hehehe。

Not related at all, but the importance of sanitizing strings: http://xkcd.com/327/ hehehe.

这篇关于如何在iPhone应用程序中使用正则表达式来分隔字符串,(逗号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆