Regex split a string

梦想与她 提交于 2021-01-28 12:11:38

问题


I am at my wit ends (being a regex novice). I need to split a string like this

"abc","","av,as","hello world","nice,name"

into

'abc'
'\blank\'
'av,as'
'hello world'
'nice,name'

Using c# or excel vbs, can someone help with the regex expression?


回答1:


Fairly straightforward:

"(\\.|[^"\\])*"

will work as shown:

Comma-delimited list of quoted strings

It will allow escaped quotes and possible whitespace between quotations, and is POSIX compliant, should you ever need that!

EDIT

I should probably note that it will basically NOT be possible to get the '\blank\' you specified directly from the regex engine, but would be relatively trivial to get it from code that checks the the length of the match and replaces it if is less than three characters long (as the match will be "" if there was an empty string)

END EDIT

Please ask if you would like me to break down the expression!




回答2:


I think you should use

"(.*?)",?

regex

Example: http://regexr.com?2uvk8




回答3:


(?:"((?:[a-z])+(?:[ ,a-z]+))")?("")? 

can be used

if group 1 and 2 are emtpy, you matched a , if group 1 has a value you matched a string and if group 2 has a value you matched emtpy double quotes ""

But yes as said, you should use a parser for speed and accuracy...



来源:https://stackoverflow.com/questions/7825496/regex-split-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!