Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Test if string is already quote()'d? (REGEX rox)

by gmpassos (Priest)
on Jun 16, 2003 at 03:04 UTC ( [id://266100]=note: print w/replies, xml ) Need Help??


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".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://266100]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2024-03-28 10:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found