Regex trick, how to remove one quoted content from a string?

Discussion in 'Perl Misc' started by Kelvin, Nov 9, 2004.

  1. Kelvin

    Kelvin Guest

    Hi, there:

    In some portions of my string, there are many quoted contents separated by ;
    sign.
    What I need is retain only one quoted content and remove the extras.

    How can regex do this job?

    For example,
    ===========================================
    $str = ___d event; "he missed the test and had to take a makeup"; "the two
    teams played a makeup one week later";;cosm___
    ===========================================
    $new_str = ___d event; "he missed the test and had to take a
    makeup";;cosm___



    Thank you in advance.
    Kelvin, Nov 9, 2004
    #1
    1. Advertising

  2. Kelvin wrote:
    >
    > In some portions of my string, there are many quoted contents separated by ;
    > sign.
    > What I need is retain only one quoted content and remove the extras.
    >
    > How can regex do this job?
    >
    > For example,
    > ===========================================
    > $str = ___d event; "he missed the test and had to take a makeup"; "the two
    > teams played a makeup one week later";;cosm___
    > ===========================================
    > $new_str = ___d event; "he missed the test and had to take a
    > makeup";;cosm___


    1 while $str =~ s/("[^"]*");[^"]*"[^"]*"/$1/;


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Nov 9, 2004
    #2
    1. Advertising

  3. Kelvin

    Anno Siegel Guest

    Kelvin <> wrote in comp.lang.perl.misc:
    > Hi, there:
    >
    > In some portions of my string, there are many quoted contents separated by ;
    > sign.


    ....plus possibly blanks, it seems. Please be precise in your
    specification.

    > What I need is retain only one quoted content and remove the extras.
    >
    > How can regex do this job?


    Here is one way (untested). Separate the string into three parts:
    What comes before the first '"', everything from the first to the last
    '"', and the remainder. Split the middle part on /; */ or whatever
    the exact separator is. Select the one you want to keep (I'll assume
    the first one). Put everything together again.

    my ( $head, $mid, $tail) = $str =~ /^([^"]*)(".*")([^"]*)$/;
    my $sel = ( split /; */, $mid)[ 0]; # select the first quoted string
    my $new_str = join '', $head, $sel, $tail;

    Anno
    Anno Siegel, Nov 9, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alain FELER

    regex trick

    Alain FELER, Feb 5, 2006, in forum: Ruby
    Replies:
    6
    Views:
    106
    Alain FELER
    Feb 7, 2006
  2. Ruby Newbee

    regex =~ string or string =~ regex?

    Ruby Newbee, Jan 4, 2010, in forum: Ruby
    Replies:
    3
    Views:
    128
    Kirk Haines
    Jan 4, 2010
  3. Patrick Drouin

    regex trick needed

    Patrick Drouin, Oct 26, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    118
    Tad McClellan
    Oct 27, 2004
  4. Replies:
    0
    Views:
    107
  5. Robert Oschler
    Replies:
    2
    Views:
    110
    peterS.
    Aug 2, 2005
Loading...

Share This Page