http://qs321.pair.com?node_id=266100


in reply to Test if string is already quote()'d?

Well, here's a regex function that I made for that:
print is_quoted(q`I'm not quoted!`) . "\n" ; print is_quoted(q`"I'm \\"quoted!"`) . "\n" ; print is_quoted(q`"I'm not quoted!\"`) . "\n" ; print is_quoted(q`"I'm not \\\\"quoted!"`) . "\n" ; print is_quoted(q`""`) . "\n" ; print is_quoted(q`''`) . "\n" ; print is_quoted(q`"x"`) . "\n" ; print is_quoted(q`'x'`) . "\n" ; ############# # IS_QUOTED # ############# sub is_quoted { my $data = shift ; print "<<$data>>\n" ; $data =~ s/\\\\//gs ; ## Ignore \\ if ( $data =~ /^ ## Begin \s* ## Ignore spaces in begin. (?:(?!\\).|) ## Quote without \ before. (?: "[^"\\]?" ## Blank quoted or unique. | "(?:(?:\\")|[^"])+(?!\\)." ## quoted with values without " insid +e, but accept \" | '[^'\\]?' ## Blank quoted or unique. | '(?:(?:\\')|[^'])+(?!\\).' ## quoted with values without ' insid +e, but accept \' ) \s* ## Ignore spaces in end. $/sx ## End. /x => extend space for the regex. ) { return( 1 ) ;} ## return true return 0 ; ## return false ##better: # return undef ; }
Note that I ignore spaces before and after the quoted values. If you don't want, just remove the \s* from the regex.

I have added the print() inside the function just to follow the tests. Here's the output:

<<I'm not quoted!>> 0 <<"I'm \"quoted!">> 1 <<"I'm not quoted!\">> 0 <<"I'm not \\"quoted!">> 0 <<"">> 1 <<''>> 1 <<"x">> 1 <<'x'>> 1

Enjoy!

Graciliano M. P.
"The creativity is the expression of the liberty".