Regex: How to ignore pairs of quotes? (beginner level question)

T

Tobias Weihmann

Hi,

I'm trying to parse VB strings here but I didn't succeed in getting
the regex to ignore pairs of double quotes (which are the equivalent to
\" in Perl)

Example:

print "This is a test ""Test"" yeah" + Str$(4)+ "good"
--> This is a test ""Test"" yeah
--> good

print """We""are""testing"""+"What they can do" '
--> ""We""are""testing""
--> What they can do

I finished off writing a FSM, but this is quite clumsy compared to a
regex. The best thing I had was like /"(.*?)"[^"]/, but this would get me
--> This is a test "
for the first example... ( the "T satisfies the end condition, even though
the two quotes should be ignored altogether)

I also considered /"(.*?[^"])"[^"]/, but this will break in the second
example - because it is's of course possible to have two quotes (to be
ignored) right before a quote finishing of the string. I also experi-
mented with non-moving sequences (?!"") but to no avail...

How would you do it?

Cheers!
Toby
 
T

Tad McClellan

Tobias Weihmann said:
Hi,

I'm trying to parse VB strings here but I didn't succeed in getting
the regex to ignore pairs of double quotes (which are the equivalent to
\" in Perl)

Example:

print "This is a test ""Test"" yeah" + Str$(4)+ "good"
--> This is a test ""Test"" yeah
--> good

print """We""are""testing"""+"What they can do" '
--> ""We""are""testing""
--> What they can do

I finished off writing a FSM, but this is quite clumsy compared to a
regex. The best thing I had was like /"(.*?)"[^"]/, but this would get me
--> This is a test "
for the first example... ( the "T satisfies the end condition, even though
the two quotes should be ignored altogether)

I also considered /"(.*?[^"])"[^"]/, but this will break in the second
example - because it is's of course possible to have two quotes (to be
ignored) right before a quote finishing of the string. I also experi-
mented with non-moving sequences (?!"") but to no avail...

How would you do it?


print "$1\n" while /"(([^"]|"")*)"/g;

or, if you want to be able to read what you've written:

print "$1\n" while /"(
( [^"] | "" )*
)"
/gx;
 
M

Malcolm Dew-Jones

Tobias Weihmann ([email protected]) wrote:
: Hi,

: I'm trying to parse VB strings here but I didn't succeed in getting
: the regex to ignore pairs of double quotes (which are the equivalent to
: \" in Perl)

: Example:

: print "This is a test ""Test"" yeah" + Str$(4)+ "good"
: --> This is a test ""Test"" yeah
: --> good

: print """We""are""testing"""+"What they can do" '
: --> ""We""are""testing""
: --> What they can do

: I finished off writing a FSM, but this is quite clumsy compared to a
: regex. The best thing I had was like /"(.*?)"[^"]/, but this would get me
: --> This is a test "
: for the first example... ( the "T satisfies the end condition, even though
: the two quotes should be ignored altogether)

: I also considered /"(.*?[^"])"[^"]/, but this will break in the second
: example - because it is's of course possible to have two quotes (to be
: ignored) right before a quote finishing of the string. I also experi-
: mented with non-moving sequences (?!"") but to no avail...

: How would you do it?

Someone else posted something like the following to a similar problem.

my example is tested, but just for the one example


# $s contains the VB string you wish to parse.

$s=q{print "This is a test ""Test"" yeah" + Str$(4)+ "good"};

# extract the quoted portions
@s= $s=~ m/"((?:[^"]|"")*)"/g;

# examine what we extracted
print map {"=>$_<=\n" } @s;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top