解析字符串中的单词 [英] parsing words in a string

查看:86
本文介绍了解析字符串中的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




是否有一个类可以处理逗号分隔字符串,例如引号中的逗号被忽略?


我知道我们可以在perl中使用Text :: ParseWords指令来执行此操作,但是我不知道
是C#.Net的新手并且找不到类似的东西。


例如


string str =" one field,two field,\" field val one,field val two,

field val three \,三个字段 ;


然后我应该在str_arr中获得以下内容

str_arr [0] =" one field"

str_arr [1] =" two filed"

str_arr [2] =" field val one,field val two,field val three"

str_arr [3 ] =三个字段


提前致谢,


Ashoo。

解决方案

string [] str_arr = str.Split(",");


string [] str_arr = str.Split('','');


不起作用。我试过了。它给了我


str_arr [0] =" one field"

str_arr [1] =" two filed"

str_arr [2] =" \" field val one"

str_arr [3] =" field val two"

str_arr [4] =" field val three \""

str_arr [5] =" three field"


而不是

str_arr [0] =一个字段

str_arr [1] =" two filed"

str_arr [2] =" field val one,field val two, field val three"

str_arr [3] =" three field"


查看str_arr [2]值。


谢谢,

Ashoo

-

通过.NET新闻组发送
http://www.dotnetnewsgroups.com


hi,

你可以使用正则表达式,它可以给你这样的结果。这是

一个正则表达式,可以获取你想要的结果。我b $ b假设您使用的是VS2003。


(?:(?< Vals>。*?),)+"(?<瓦尔斯> *)"?(?:?,(小于瓦尔斯> *。?))+

Hi,

Is there a class that can handle splitting of a string on a comma such
that the commas in quotes are ignored?

I know we can use Text::ParseWords directive in perl to do this, but I
am new to C#.Net and couldn''t find anything similar.

For example

string str = "one field, two field, \"field val one, field val two,
field val three\", three field" ;

Then I should get the following in a str_arr

str_arr[0] = "one field"
str_arr[1] = "two filed"
str_arr[2] = "field val one, field val two, field val three"
str_arr[3] = "three field"

Thanks in advance,

Ashoo.

解决方案

string[] str_arr = str.Split(",");


string [] str_arr = str.Split('','');

does not work. I had tried it. It gives me

str_arr[0] = "one field"
str_arr[1] = "two filed"
str_arr[2] = "\"field val one"
str_arr[3] = "field val two"
str_arr[4] = "field val three\""
str_arr[5] = "three field"

Instead of
str_arr[0] = "one field"
str_arr[1] = "two filed"
str_arr[2] = "field val one, field val two, field val three"
str_arr[3] = "three field"

look at the str_arr[2] value.

Thanks,
Ashoo
--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com


hi,
you can use Regular Expression which can give u such results. here is
one regular expression which will fetch you your desired result. i
assumed that you are using VS2003.

(?:(?<Vals>.*?),)+"(?<Vals>.*?)"(?:,(?<Vals>.*?))+


这篇关于解析字符串中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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