Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting rid of inverted commas around a string.

5 views
Skip to first unread message

Fea...@gmail.com

unread,
May 25, 2006, 6:20:40 AM5/25/06
to
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.

delfi...@homoludens.elte.hu

unread,
May 25, 2006, 6:44:30 AM5/25/06
to

#which one do you like? what do you really want?


foreach $a ('" hello world "','hello world','hello world"','"hello world','"hello"world"') {
$a3=$a2=$a1=$a0=$a;

chop($a0); # remove last char

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

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

$a3= (split(/\"/,$a))[1] ||'undef'; #extract string between the first and second invertedcomas

print "a:-$a-\na0:-$a0-\na1:-$a1-\na2:-$a2-\na3:-$a3-\n\n";

};


# Bye delfin


Mirco Wahab

unread,
May 25, 2006, 7:54:54 AM5/25/06
to
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

this extracts all of the text from within the ""-s

before:
print "|$string|\n"; # prints | " hello world " |
after:
print "|$string|\n"; # prints | hello world |

Regards

Mirco

A. Sinan Unur

unread,
May 25, 2006, 8:22:02 AM5/25/06
to
Mirco Wahab <wa...@chemie.uni-halle.de> wrote in news:e5465d$s73$1
@mlucom4.urz.uni-halle.de:

> Hi Fearban
>> 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.
>
> 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 <1u...@llenroc.ude.invalid>
(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

Mirco Wahab

unread,
May 25, 2006, 8:53:58 AM5/25/06
to
Hi A. Sinan Unur

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

So I fixed his problem-underspecification by this ;-)
As you know, this example also works on the very string
as given by the OP.

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

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

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

of course, TMTOWTDI, consider:

$string = [split /\s*\"+/, $string]->[1];


Regards

Mirco

A. Sinan Unur

unread,
May 25, 2006, 9:12:51 AM5/25/06
to
Mirco Wahab <wa...@chemie.uni-halle.de> wrote in news:e549k5$t70$1
@mlucom4.urz.uni-halle.de:

> Hi A. Sinan Unur

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



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

...

>> Also, I am not sure why both you and delfin feel the need to
>> backslash quotation marks in the pattern.
>
> 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";

Bernard El-Hagin

unread,
May 25, 2006, 9:20:02 AM5/25/06
to
Mirco Wahab <wa...@chemie.uni-halle.de> wrote:

[...]

> of course, TMTOWTDI, consider:
>
> $string = [split /\s*\"+/, $string]->[1];


Apart from being simply *appalling*, that is also wrong.

------------


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

print "[$string]\n";
------------

[ hello world ]


------------


my $string = qq{ " hello world "\n };

$string = [split /\s*\"+/, $string]->[1];

print "[$string]\n";
------------

[ hello world]


--
Cheers,
Bernard

Mirco Wahab

unread,
May 25, 2006, 9:45:09 AM5/25/06
to
Hi Bernard

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

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

...


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

$string =~ s/^\s*"+(.*)"+[^"]*$/$1/;
...

as you and S.A. Unur already said ...
(what one can express in one line or in two lines, as everybody likes).

Regards

Mirco

Mirco Wahab

unread,
May 25, 2006, 9:49:42 AM5/25/06
to
Hi A. Sinan Unur

>> [\"]-problem


>
> 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*$"";

This is exactly what I tried to express.

Sometimes, people do some things that appear
to other people not 'correct enough', the
other people will give hints or complaints,
these will then be appreciated and life goes on ;-)

> Say what you mean, and mean what you say.

> ...


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

Did you quote \" intentionally or
accidentially?

Regards

Mirco

A. Sinan Unur

unread,
May 25, 2006, 9:58:53 AM5/25/06
to
Mirco Wahab <wa...@chemie.uni-halle.de> wrote in news:e54ck5$v8$1
@mlucom4.urz.uni-halle.de:

> 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

unread,
May 25, 2006, 10:01:17 AM5/25/06
to
Mirco Wahab <wa...@chemie.uni-halle.de> wrote in news:e54csn$10g$1
@mlucom4.urz.uni-halle.de:

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

delfi...@homoludens.elte.hu

unread,
May 25, 2006, 10:05:15 AM5/25/06
to
In article <e549k5$t70$1...@mlucom4.urz.uni-halle.de>, Mirco Wahab <wa...@chemie.uni-halle.de> writes:
[...]

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

No. It's neither a feeling nor a need.
It's a style.
It's easiest to read or write \" that mean it's a single quoted character
whithout any perl meaning. (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.
Easier to change code
eg:

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

whitout changing the representation of \" inside the regular or string expression.

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.

Finally, the most primitive syntax highlighting works better on /\"/ also

Bye delfin...

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

[...]
>
>
> Regards
>
> Mirco

A. Sinan Unur

unread,
May 25, 2006, 10:15:04 AM5/25/06
to
delfi...@homoludens.elte.hu wrote in news:wcUIBIzDa1RF@ludens:

> In article <e549k5$t70$1...@mlucom4.urz.uni-halle.de>, Mirco Wahab
> <wa...@chemie.uni-halle.de> writes: [...]
>>> Also, I am not sure why both you and delfin feel the need to
>>> backslash quotation marks in the pattern.

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

Mirco Wahab

unread,
May 25, 2006, 10:08:51 AM5/25/06
to
H Sinan,

> *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

Oops, this is an Idiom already, I din't
realize that one ...

>>(what one can express in one line or in two lines, as everybody
>>likes).
>
> I do not know what you mean by that.

As long as it works for one and
as long as one is happy with it,
he should do that.

Regards

Mirco

PS.: btw. what do you mean by "quote correct
attributions"? - what doesn't work for you?

A. Sinan Unur

unread,
May 25, 2006, 10:24:34 AM5/25/06
to
Mirco Wahab <wa...@chemie.uni-halle.de> wrote in news:e54e0i$1bb$1
@mlucom4.urz.uni-halle.de:

> PS.: btw. what do you mean by "quote correct
> attributions"? - what doesn't work for you?

Take a look at the first line of this message. What do you see? Now, take
a look at the replies you have posted. Do you see any attributions?

http://cfaj.freeshell.org/google/

Tad McClellan

unread,
May 25, 2006, 10:51:51 AM5/25/06
to

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

> $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)


--
Tad McClellan SGML consulting
ta...@augustmail.com Perl programming
Fort Worth, Texas

delfi...@homoludens.elte.hu

unread,
May 25, 2006, 11:38:03 AM5/25/06
to
In article <Xns97CE68523A47...@127.0.0.1>, "A. Sinan Unur" <1u...@llenroc.ude.invalid> writes:
> delfi...@homoludens.elte.hu wrote in news:wcUIBIzDa1RF@ludens:
>
>> In article <e549k5$t70$1...@mlucom4.urz.uni-halle.de>, Mirco Wahab
>> <wa...@chemie.uni-halle.de> writes: [...]
>>>> Also, I am not sure why both you and delfin feel the need to
>>>> backslash quotation marks in the pattern.
>
> 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
article <Xns97CE68523A47...@127.0.0.1>, "A. Sinan Unur" <1u...@llenroc.ud`
[...]

> It is a bogus style.
[...]

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

my syntax highlighter misinterprete q{}
so it's easier, and more productive to use the "\"..." style.
well we might talk about my editor but that's offtopic :)

>
> Also, you do realize that the two conditions are fundamentally
> different, right?
>

exactly.

[...]

Mirco Wahab

unread,
May 25, 2006, 1:50:03 PM5/25/06
to
Thus spoke A. Sinan Unur (on 2006-05-25 16:24):
> Mirco Wahab <wa...@chemie.uni-halle.de> wrote in news:e54e0i$1bb$1...@mlucom4.urz.uni-halle.de:

>> PS.: btw. what do you mean by "quote correct
>> attributions"? - what doesn't work for you?
>
> Take a look at the first line of this message. What do you see? Now, take
> a look at the replies you have posted. Do you see any attributions?

WTF ... uhhh, I lost the obvious when switching the news agents
and didn't even note that ...

Thanks for ponting my nose at it.

Regards

Mirco

A. Sinan Unur

unread,
May 25, 2006, 2:10:43 PM5/25/06
to
Mirco Wahab <wa...@chemie.uni-halle.de> wrote in
news:e54qvb$511$1...@mlucom4.urz.uni-halle.de:

Thanks for fixing it ;-)

0 new messages