Re: How to Parse a string with Embedded Double Quotes

From: Charles Law (blank_at_nowhere.com)
Date: 11/30/04


Date: Tue, 30 Nov 2004 11:09:38 -0000

Hi Robby

Thanks for the reply. I am not sure that I understand the regular expression

> (\s*"([\s\w]*)")|(\s*(\w+))

I tried the following, but of course it gives a syntax error because of the
embedded double quotes:

    Dim reg As Regex = New Regex("(\s*"([\s\w]*)")|(\s*(\w+))")

So I tried escaping the double quotes, like this

    Dim reg As Regex = New Regex("(\s*""([\s\w]*)"")|(\s*(\w+))")

but this cleared my string out to a couple of spaces when I did a replace.

Any chance of a small snippet to get me on the right track, using the Match
object?

Thanks very much.

Charles

"Robby" <edmund@not.my.email.com> wrote in message
news:eCCPHps1EHA.3392@TK2MSFTNGP10.phx.gbl...
>
> Try
>
> (\s*"([\s\w]*)")|(\s*(\w+))
>
> Then do a Replace on each Match object with
>
> $2$4
>
> This will return either your double qouted string with out the qoutes or
> the word token without the whitespace characters depending on which match
> the Match object holds.
>
> You just have to love Regular Expressions.
>
> --Robby
>
>
>
>
> "Charles Law" <blank@nowhere.com> wrote in message
> news:OPAV%23Sm1EHA.3336@TK2MSFTNGP11.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" <notmyfirstname@planet.nl> wrote in message
>> news:OX3$bzk1EHA.2824@TK2MSFTNGP09.phx.gbl...
>>> 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" <blank@nowhere.com>
>>>
>>> ...
>>>>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
>>>>
>>>>
>>>
>>>
>>
>>
>
>



Relevant Pages

  • Re: Beginners Program
    ... I'd put single quotes aroung the, ... If you are putting "s in a string then it is usually best to use a different ... input file things could go wrong. ... and that the final line of input does end with a newline character. ...
    (comp.lang.perl.misc)
  • Small regular expression parser
    ... the goal was to develop a very simple regular expression parser. ... sets are selected using the % character instead of \. ... into the string of the start of the match and the length of the match. ... Last there are a couple macros to help with captures. ...
    (comp.lang.lisp)
  • Re: Can an "Update" Query insert quotation marks around text?
    ... quotes) is because the software is reacting to the presence of your quotes. ... that text string is delimited by " characters within the software, ... software doubles the " character because that tells the software that the " ... and the last " character marks the end of the text string. ...
    (microsoft.public.access.queries)
  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... abandoned it because I don't think Beautiful Code can be written in C: ... Pike's code doesn't implement a regular expression interpreter ... it makes no provision for a character which must occur at ... changeable index into the string it points-at and it expects the user ...
    (comp.programming)
  • Re: Checking last character of string for punctuation
    ... I'm a newbie with a newbie question. ... the string should be kept as is. ... This matches your string against the regular expression that you need to put ... Given the description of your problem, you might be interested in character ...
    (perl.beginners)