如何使用嵌入式双引号解析字符串 [英] How to Parse a string with Embedded Double Quotes
问题描述
我有一个类似于以下内容的字符串:
" MyString 40Hello world所有
它包含空格,可能是空格或制表符,或者是一个组合,我想要生成一个包含以下元素的数组
arr(0)=" MyString"
arr(1)= 40
arr(2)=" Hello world"
arr(3)=" all"
使用修剪和正则表达式(\s +),我可以将我的字符串减少到
" MyString 40" Hello world"所有
和Split我可以得到
arr(0)=" MyString"
arr (1)= 40
arr(2)="" Hello"
arr(3)=" world""
arr(4)=" all"
正如你所看到的,它不是我需要的。 Hello world
中的空格已减少到单个空格,而Split不会尊重双重
引号,并将Hello world分割为Hello world。超过两个元素。
有谁知道我该怎么做?我可以按字符处理字符串
,但是我希望有一个直接的
技术来做,没有循环,并使用一些技术我已经拥有
了。
TIA
Charles
I have a string similar to the following:
" MyString 40 "Hello world" all "
It contains white space that may be spaces or tabs, or a combination, and I
want to produce an array with the following elements
arr(0) = "MyString"
arr(1) = 40
arr(2) = "Hello world"
arr(3) = "all"
Using trim and a regular expression ("\s+"), I can reduce my string to
"MyString 40 "Hello world" all"
and with Split I can get
arr(0) = "MyString"
arr(1) = 40
arr(2) = ""Hello"
arr(3) = "world""
arr(4) = "all"
As you can see, it is not quite what I need. The spaces in "Hello world"
have been reduced to a single space, and Split does not respect the double
quotes, and splits "Hello world" over two elements.
Does anyone have an idea how I could do this? I could process the string
character by character, but I am hoping that there is a straight-forward
technique for doing it, without looping, and using some of the techniques I
already have.
TIA
Charles
推荐答案
Charles,
我正在看问题,我想我会给出答案,因为
它是如此难以形容。比我看到那是你。因此它不应该是一个问题。
在这种情况下我更换我不会用的空间
绝对未使用的角色。
拆分
然后再将未使用的角色替换为空间
我认为这对你来说是足够的解释。
现在你读了这个你说的,我也知道。
:-)))
Cor
" Charles Law" < bl *** @ nowhere.com>
....
Charles,
I was looking at the problem, I was thinking will I give my answer because
it is so difficult to describe. Than I saw that it was you. Therefore it
should not be a problem.
In this kind of situations I replace the spaces I will not use for an
absolute unused character.
Do the split
And replace the unused character again back for a space
I assume that this is for you more than enough explanation.
And now you read this you say, I knew that as well.
:-)))
Cor
"Charles Law" <bl***@nowhere.com>
....
我有一个类似于以下内容的字符串:
MyString 40Hello world所有
它包含空格,可能是空格或制表符,或组合,并且
我想生成一个包含以下元素的数组
arr (0)=" MyString"
arr(1)= 40
arr(2)=" Hello world"
arr(3)=" all"
<使用trim和正则表达式(\s +),我可以将我的字符串减少到
MyString 40Hello world"所有的
和Split我可以得到
arr(0)=" MyString"
arr(1)= 40
arr(2 )=""" Hello"
arr(3)=" world""
arr(4)=" all
如您所见,它不是我需要的。 Hello world中的空格已缩小为单个空格,而Split不会尊重双重引号,并将Hello world分割为Hello world。超过两个元素。
有谁知道我该怎么做?我可以逐个字符地处理字符串,但是我希望有一种直接的技术可以做到这一点,没有循环,并且使用了我已经拥有的一些技术。
TIA
Charles
I have a string similar to the following:
" MyString 40 "Hello world" all "
It contains white space that may be spaces or tabs, or a combination, and
I want to produce an array with the following elements
arr(0) = "MyString"
arr(1) = 40
arr(2) = "Hello world"
arr(3) = "all"
Using trim and a regular expression ("\s+"), I can reduce my string to
"MyString 40 "Hello world" all"
and with Split I can get
arr(0) = "MyString"
arr(1) = 40
arr(2) = ""Hello"
arr(3) = "world""
arr(4) = "all"
As you can see, it is not quite what I need. The spaces in "Hello world"
have been reduced to a single space, and Split does not respect the double
quotes, and splits "Hello world" over two elements.
Does anyone have an idea how I could do this? I could process the string
character by character, but I am hoping that there is a straight-forward
technique for doing it, without looping, and using some of the techniques
I already have.
TIA
Charles
Charles,
>
我正在研究这个问题,我想我会给出答案,因为
很难描述。比我看到那是你。因此它不应该是一个问题。
在这种情况下我更换我不会用的空间
绝对未使用的角色。
拆分
然后再将未使用的角色替换为空间
我认为这对你来说是足够的解释。
现在你读了这个你说的,我也知道。
:-)))
Cor
" Charles Law" < bl *** @ nowhere.com>
....
Charles,
I was looking at the problem, I was thinking will I give my answer because
it is so difficult to describe. Than I saw that it was you. Therefore it
should not be a problem.
In this kind of situations I replace the spaces I will not use for an
absolute unused character.
Do the split
And replace the unused character again back for a space
I assume that this is for you more than enough explanation.
And now you read this you say, I knew that as well.
:-)))
Cor
"Charles Law" <bl***@nowhere.com>
....
我有一个类似于以下内容的字符串:
MyString 40Hello world所有
它包含空格,可能是空格或制表符,或组合,并且
我想生成一个包含以下元素的数组
arr (0)=" MyString"
arr(1)= 40
arr(2)=" Hello world"
arr(3)=" all"
<使用trim和正则表达式(\s +),我可以将我的字符串减少到
MyString 40Hello world"所有的
和Split我可以得到
arr(0)=" MyString"
arr(1)= 40
arr(2 )=""" Hello"
arr(3)=" world""
arr(4)=" all
如您所见,它不是我需要的。 Hello world中的空格已缩小为单个空格,而Split不会尊重双重引号,并将Hello world分割为Hello world。超过两个元素。
有谁知道我该怎么做?我可以逐个字符地处理字符串,但是我希望有一种直接的技术可以做到这一点,没有循环,并且使用了我已经拥有的一些技术。
TIA
Charles
I have a string similar to the following:
" MyString 40 "Hello world" all "
It contains white space that may be spaces or tabs, or a combination, and
I want to produce an array with the following elements
arr(0) = "MyString"
arr(1) = 40
arr(2) = "Hello world"
arr(3) = "all"
Using trim and a regular expression ("\s+"), I can reduce my string to
"MyString 40 "Hello world" all"
and with Split I can get
arr(0) = "MyString"
arr(1) = 40
arr(2) = ""Hello"
arr(3) = "world""
arr(4) = "all"
As you can see, it is not quite what I need. The spaces in "Hello world"
have been reduced to a single space, and Split does not respect the double
quotes, and splits "Hello world" over two elements.
Does anyone have an idea how I could do this? I could process the string
character by character, but I am hoping that there is a straight-forward
technique for doing it, without looping, and using some of the techniques
I already have.
TIA
Charles
Hi Cor
>
你读了我的想法;-)
我曾想过使用类似#的东西,因为它永远不会出现在我的
字符串中。但后来我开始研究如何知道哪些空格要用#替换为
,哪些空格要离开。当然,对于人眼而言,显而易见的是,我只能替换人与人之间的空间。和,但现在我回到
逐字符处理字符串的每个部分,以便我正确地匹配
双引号,这就是我在尝试要避免。
也许有一个匹配双引号的正则表达式,或者一个解析字符串的
方法,但遗憾的是我不要
知道它。
但请保持建议流畅。
查尔斯>
" Cor Ligthert" <无************ @ planet.nl>在消息中写道
新闻:OX ************** @ TK2MSFTNGP09.phx.gbl ...
Hi Cor
You read my mind ;-)
I had thought of using something like #, as it will never occur in my
string. But then I started to look at how I would know which spaces to
replace with #, and which to leave. Of course, to the human eye it is
obvious that I only replace the spaces between " and ", but now I am back to
processing each part of the string character by character so that I match
double quotes correctly, and this is what I was trying to avoid.
Perhaps there is a regex expression that will match double quotes, or a
method that parses a string taking these into account, but sadly I do not
know it yet.
But please, keep the suggestions flowing.
Charles
"Cor Ligthert" <no************@planet.nl> wrote in message
news:OX**************@TK2MSFTNGP09.phx.gbl...
Charles,>
我在看问题,我想我会给出答案,因为它很难描述。比我看到那是你。因此它不应该是一个问题。
在这种情况下,我会替换我不会用于
绝对未使用的字符的空格。
>做分裂
然后再将未使用的角色替换回来以获得空间
我认为这对你来说已经足够了解。
现在你读到这个你说,我也知道。
:-)))
查尔斯劳 < bl *** @ nowhere.com>
...
Charles,
I was looking at the problem, I was thinking will I give my answer because
it is so difficult to describe. Than I saw that it was you. Therefore it
should not be a problem.
In this kind of situations I replace the spaces I will not use for an
absolute unused character.
Do the split
And replace the unused character again back for a space
I assume that this is for you more than enough explanation.
And now you read this you say, I knew that as well.
:-)))
Cor
"Charles Law" <bl***@nowhere.com>
...
我有一个类似于以下内容的字符串:
" MyString 40Hello world所有
它包含空格,可能是空格或制表符,或组合,并且
我想生成一个包含以下元素的数组
arr (0)=" MyString"
arr(1)= 40
arr(2)=" Hello world"
arr(3)=" all"
<使用trim和正则表达式(\s +),我可以将我的字符串减少到
MyString 40Hello world"所有的
和Split我可以得到
arr(0)=" MyString"
arr(1)= 40
arr(2 )=""" Hello"
arr(3)=" world""
arr(4)=" all
如您所见,它不是我需要的。 Hello
world中的空格已被缩减为单个空格,斯普利特不尊重双引号,并将Hello world分割为Hello world。超过两个元素。
有谁知道我该怎么做?我可以逐个字符地处理字符串,但是我希望有一种直接的技术可以做到这一点,没有循环,并且使用了我已经拥有的一些技术。
TIA
Charles
I have a string similar to the following:
" MyString 40 "Hello world" all "
It contains white space that may be spaces or tabs, or a combination, and
I want to produce an array with the following elements
arr(0) = "MyString"
arr(1) = 40
arr(2) = "Hello world"
arr(3) = "all"
Using trim and a regular expression ("\s+"), I can reduce my string to
"MyString 40 "Hello world" all"
and with Split I can get
arr(0) = "MyString"
arr(1) = 40
arr(2) = ""Hello"
arr(3) = "world""
arr(4) = "all"
As you can see, it is not quite what I need. The spaces in "Hello
world" have been reduced to a single space, and Split does not respect
the double quotes, and splits "Hello world" over two elements.
Does anyone have an idea how I could do this? I could process the string
character by character, but I am hoping that there is a straight-forward
technique for doing it, without looping, and using some of the techniques
I already have.
TIA
Charles
这篇关于如何使用嵌入式双引号解析字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!