Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Perl: the Markov chain saw
 
PerlMonks  

Split a string to remove quotes and parentheses

by perl_99_monk (Novice)
on Feb 20, 2006 at 16:49 UTC ( #531458=perlquestion: print w/ replies, xml ) Need Help??
perl_99_monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am trying to split a string having ("test123") I want to remove the braces and quotes and just need test123. is there a way to accomplish this? I tried splitting on ( and " but getting errors...

2006-02-20 Retitled by GrandFather, as per Monastery guidelines
Original title: 'Split'

Comment on Split a string to remove quotes and parentheses
Re: Split a string to remove quotes and parentheses
by vladb (Vicar) on Feb 20, 2006 at 16:55 UTC
    You can try perl search/replace regexp:
    $s = '("test123")'; $s =~ s/(\("|"\))//g; print "$s\n";
    Will print:
    test123
    


    _____________________
    "We've all heard that a million monkeys banging on a million typewriters will eventually reproduce
    the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true."

    Robert Wilensky, University of California

      Thank you so much... It worked
Re: Split a string to remove quotes and parentheses
by ikegami (Pope) on Feb 20, 2006 at 16:56 UTC
    split is most useful when dealing with a list of items. In this case you only have one (test123), so you should look to regexps instead. The following snippets will do:
    $string =~ s/^\("(.*)"\)$/$1/;
    # Faster than first. $string =~ s/^\("//; $string =~ s/"\)$//;
    # Same as previous, less redundant. s/^\("//, s/"\)$// for $string;

    Update: I didn't know the braces needed to be replaced. I'm fixed my snippets. Two following two snippet cann't be fixed:

    # You didn't mention an escape mechanism, so I'm # assuming there's no other quotes in the string. $string =~ s/"//g;
    # I didn't say it was impossible. $string = (split '"', $string)[1];
Re: Split a string to remove quotes and parentheses
by CountOrlok (Friar) on Feb 20, 2006 at 16:56 UTC
    Is '("test123")' all that is to be split on the line? Use a regex instead of split. There are a hundred ways to write it, but here is one:
    if ($string =~ /\(\"(.*+)\"\)/) { $matched = $1; }
    -imran
Re: Split a string to remove quotes and parentheses
by blue_cowdawg (Prior) on Feb 20, 2006 at 17:02 UTC
        I want to remove the braces and quotes and just need test123.

    <Maine Downeaster Accent>
    Don' think ya can get theah from heah!
    </Maine Downeaster Accent>
    Split is on the answer you are after for that application. Try this:

    $string='"(test123)"'; $string =~ s/[\"\(\)]//g; printf "%s\n",$string;
    You'll be glad you did.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Split a string to remove quotes and parentheses
by explorer (Chaplain) on Feb 20, 2006 at 17:08 UTC
    Radical look:
    $s = '("test123")'; $s =~ s/\W//g; print "$s\n"
Re: Split a string to remove quotes and parentheses
by graff (Chancellor) on Feb 21, 2006 at 06:11 UTC
    You say you are trying to "split" the string, but it looks like you just want to remove unwanted characters. Everyone has pointed out variations on  s/[()"]+//g which certainly works.

    Another way is the "tr" operator, which simply does character transliterations. In this case, you can "transliterate" the unwanted characters to nothing -- i.e. delete them:

    my $string = '"(test123)"'; $string =~ tr/()"//d;
    This is a simpler operation than regex substitution, so if your code needs to do this a lot, you could see a performance difference.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://531458]
Approved by wfsp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2014-04-17 10:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (444 votes), past polls