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".
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|