Solved

perl, strip spaces and  double quote character at beginning and at the end of a string

Posted on 2010-08-19
10
561 Views
Last Modified: 2012-08-13
How do I strip spaces and double quote character at beginning and at the end of a string, if the double quote exist?

$string1 = "\"my string1\"";  # => to "my string1"
$string2 = "  \" my string2 \"  "; #=> to " mystring2 "
0
Comment
Question by:rgbcof
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 10

Accepted Solution

by:
jeromee earned 63 total points
ID: 33478895
Please try this:
   s/(^\s*")|("\s*$)//g;
0
 
LVL 3

Expert Comment

by:YellowShoe
ID: 33479545
Does '\' specify the part where the string starts/ends. If so you could use that as a delimiter as well.

Using your logic - if double quotes and spaces appear at the start and end of strong
$string1 =~ s/^[ "]*//; # delete all spaces and double quotes 0 0r more occurrences
$string1 =~ s/[ "]*$//;

alternately
$string1 = /^[^"]*([^\\]*)\\".*/$1"/;
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 33479555
This should do it:

$string =~ s{^[\s"]+}{};
$string =~ s{[\s"]+$}{};

Using | as jeromee did above will sometimes work and sometimes not.  For example,

$string =~ s{(?:^[\s"]+)|([\s"]+$)}{};

does not work.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Assisted Solution

by:ozo
ozo earned 62 total points
ID: 33479973
s/^\s*"//,s/"\s*$// for $string1,$string2 ;
0
 
LVL 84

Expert Comment

by:ozo
ID: 33480017
$string =~ s{(?:^[\s"]+)|([\s"]+$)}{}g; #works, but the g is required, and it is not as fast as s{^[\s"]+}{} and s{[\s"]+$}{}; separately, if you care about that
also, it does
$string2 = "  \" my string2 \"  "; #=> to "mystring2"
unlike the requested
$string2 = "  \" my string2 \"  "; #=> to " mystring2 "
0
 
LVL 10

Expert Comment

by:jeromee
ID: 33480100
I think the initial answer I posted:
   s/(^\s*")|("\s*$)//g;
works for both strings.
0
 
LVL 84

Expert Comment

by:ozo
ID: 33480145
Yes, it does work for both strings.
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 33480712
sigh - yep - I made two mistakes in my comment as ozo pointed out.  I left off the g on the "doesn't work" example (so it does work) and I misread the request so used char classes which are efficient but don't work in this case.

jeromee, yes, your answer worked.  I should have been more clear - I was trying to give a different example that didn't work (and failed at that as well).

Apparently my brain is dead today...
0
 

Author Closing Comment

by:rgbcof
ID: 33480905
You guys are such perl/regex expert.  I am so jealous.
0
 
LVL 10

Expert Comment

by:jeromee
ID: 33480938
If you play with regex regularly it gets easier, and it quickly loses its "crypticness", I promise!

Good luck.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Add additional column to .csv using Perl. 8 177
Matching a random pattern with one common character 2 100
read an xml file in perl 2 87
make a default program in ubuntu 6 25
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question