Getting rid of inverted commas around a string.

F

Fearban

Hi

How can one get rid of double inverted commas aound a string

" hello world "

I know that chop will remove the tailing double inverted comma
but what about the head.
 
A

A. Sinan Unur

Hi Fearban

The 'hungarian delfin' did give
you already a more or less complex
solution, but I'll add a short snippet
here which illustrates the point:

example:

my $string = q{ " hello world " }; # | " hello world " |
$string =~ s/\s*\"(.*)\"/$1/m; # extract (*) from quotes

The OP wanted to remove leading or trailing double-quotation marks. He
did not specify that there are no quotation marks within the string. He
also did not mention anything about leading or trailing spaces.

Also, I am not sure why both you and delfin feel the need to backslash
quotation marks in the pattern.

If one wants to remove leading quotation marks (optionally preceded by
spaces) or trailing quotation marks (optionally followed by spaces),
here is how to do it:

#!/usr/bin/perl

use strict;
use warnings;

my $string = qq{ " hello world "\n };
$string =~ s/^\s*"+//;
$string =~ s/"+\s*$//;

print qq{'$string'\n};

__END__

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
A

A. Sinan Unur

Hi A. Sinan Unur

[ Please preserve proper attributions when you reply. The text preceded
by >>> was written by you ]
....


This is some habit of myself, mostly because people
sometimes tend to quote "regexp's" by different
quotation marks themselves, in order to control
interpolation (in which case the above example could
fail without quoting the quotation marks, imho).

That makes no sense. You did not use " as a pattern delimiter. If you
had written

my $string = qq{ " hello world "\n };
$string =~ s"^\s*\"+"";
$string =~ s"\"+\s*$"";

I would have questioned your choice of a delimiter that requires that
you backslash more than necessary.

On the other hand, if one did not want interpolation (which makes no
difference here because there is nothing to be interpolated in the
pattern), one would have used:

my $string = qq{ " hello world "\n };
$string =~ s'^\s*"+'';
$string =~ s'"+\s*$'';
$string = [split /\s*\"+/, $string]->[1];

Only if you are into obfuscation, and only if no valid quotation marks
can appear in the middle of the string. Say what you mean, and mean what
you say.

#!/usr/bin/perl

use strict;
use warnings;

my $string = qq{" How about "this"? "};

my $result = (split /\s*\"+/, $string)[1];

print "$result\n";
__END__



--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
A

A. Sinan Unur

Hi Bernard

[ Once again, *please* preserve attributions when you reply. ]
$string = [split /\s*\"+/, $string]->[1];
Apart from being simply *appalling*, that is also wrong.
...
print "[$string]\n";

[ hello world]

Of course is the split solution not appropriate to
the said problem (rather obscure), but it might be
the best choice if the OP specifies that his string
consists of lots of those "aaa" "bbb" "ccc"-things.

#!/usr/bin/perl

use strict;
use warnings;

use Text::parseWords;

my $string = q{"a\"a\"a" "bbb" "ccc" "ddd"};

my @words = shellwords($string);

print "$_\n" for @words;

__END__

What was meant in the specification is
*probably* something like this:

...
my $string = qq{ " How about this? "};
$string =~ s/^\s*"+(.*)"+[^"]*$/$1/;

No, the specification (however poorly worded) was to remove leading and
trailing quotation marks.

*The* solution to that problem is no different than the problem of
removing any leading or trailing characters:

$string =~ s/^"+//;
$string =~ s/"+$//;

perldoc -q space
as you and S.A. Unur already said ...

http://wordnet.princeton.edu/perl/webwn?s=sa

You could have just called me as Sinan.
(what one can express in one line or in two lines, as everybody
likes).

I do not know what you mean by that.

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
A

A. Sinan Unur

Hi A. Sinan Unur

[ Preserve attributions when you quote. ]
...
my $result = (split /\s*\"+/, $string)[1];

Did you quote \" intentionally or
accidentially?

I copied and pasted your code. I was focused on removing the unnecessary
array-ref stuff, so I missed the escaped quotation mark.

Sinan
--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
A

A. Sinan Unur

Here we go again: It wasn't Mirco Wahab who wrote that. I did:

http://groups.google.com/group/comp.lang.perl.misc/msg/abf08232b414edac
No. It's neither a feeling nor a need.
It's a style.

It is a bogus style.
It's easiest to read or write \" that mean it's a single quoted
character whithout any perl meaning.

ITYM double quotation mark.
(doesn't matter if it is in "..." or in /.../) there is no need to
really make effort to keep in mind the current context (string,
regexp, etc) so it's faster.

It indicates that you do not completely understand what you are doing.

if ($x =~ /\"quoted\" string/) ...
into
if ($x eq "\"quoted\" string") ...

Better:

if ( $x eq q{"quoted" string} ) {

Also, you do realize that the two conditions are fundamentally
different, right?
Everyone understand it who write
$hash{key1}->{key2}
instead of
$hash{key1}{key2}

they are also equivalent, and not always the shorter is the better.

No one claimed "shorter is better".

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
A

A. Sinan Unur

T

Tad McClellan

$a1=~ s/\"//g; # remove _all_ inverted_commas


You shouldn't backslash characters that don't need to be backslashed.

tr/// is better when you are working on characters rather than on strings:

$a1 =~ tr/"//d; # remove _all_ double quotes

$a2=~ s/^\"//; # remove only the first (if exists)


Your comment is misleading.

$a2 =~ s/"//; # remove only the first (if exists)

or

$a2 =~ s/^"//; # remove only the first (if exists at beginning of string)
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top