upaksh has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I have a string which can has a value like,
my $str = "auth_plugin_stack = a,b,EXT::USCC::USCCAuth,c,d"; # should +return auth_plugin_stack = a,b,c,d
I want to have a regex to remove the word 'EXT::USCC::USCCAuth'. The $str can have following values also.
my $str = "auth_plugin_stack = EXT::USCC::USCCAuth"; # should return a +uth_plugin_stack = my $str = "auth_plugin_stack = a,b,EXT::USCC::USCCAuth"; # should retu +rn auth_plugin_stack = a,b my $str = "auth_plugin_stack = EXT::USCC::USCCAuth,c,d"; # should retu +rn auth_plugin_stack = c,d
Thanks.

Replies are listed 'Best First'.
Re: regex to remove a word from string
by davido (Cardinal) on Dec 04, 2014 at 05:44 UTC

    As this is your tenth post, I feel someone ought to help you out by pointing you to Writeup Formatting Tips. Well formatted posts get better answers.

    The following code does what it seems that you're asking.

    my $str = "auth_plugin_stack = a,b,EXT::USCC::USCCAuth,c,d"; $str =~ s/,EXT::USCC::USCCAuth(?=,)//; print "$str\n";

    This prints the following output:

    auth_plugin_stack = a,b,c,d

    If the literal string "EXT::USCC::USCCAuth" is too specific when used as a pattern, you might need to explain what additional challenges you're facing.

    Between perlintro and perlretut, you should have adequate ammunition to bag this task, unless there's more to it than you've told us.

    By the way, this string looks like it might have come from a CSV file. If that's the case, you may be happy to discover Text::CSV and Text::CSV_XS.

    Update: Wait a second... I feel duped here. Your profile says "PERL developer having 5+ years exp." Surely there's more to your question than how to construct a regular expression consisting entirely of a literal string. A Perl developer claiming 5+ years of experience must have a deeper question than this. What are we missing that we need to be told, in order to provide a useful answer to the sort of question that someone of your experience would ask?


    Dave

Re: regex to remove a word from string
by johngg (Canon) on Dec 04, 2014 at 10:13 UTC

    An alternative using splits and joins.

    $ perl -Mstrict -Mwarnings -E ' my @strings = ( q{auth_plugin_stack = EXT::USCC::USCCAuth}, q{auth_plugin_stack = a,b,EXT::USCC::USCCAuth,c,d}, q{auth_plugin_stack = a,b,EXT::USCC::USCCAuth}, q{auth_plugin_stack = EXT::USCC::USCCAuth,c,d}, ); foreach my $string ( @strings ) { my( $left, $right ) = split m{ = }, $string; say join q{ = }, $left, join q{,}, grep { $_ ne q{EXT::USCC::USCCAuth} } split m{,}, $right; }' auth_plugin_stack = auth_plugin_stack = a,b,c,d auth_plugin_stack = a,b auth_plugin_stack = c,d $

    I hope this is of interest.

    Cheers,

    JohnGG

Re: regex to remove a word from string
by mendeepak (Scribe) on Dec 04, 2014 at 05:55 UTC

    I do not know whether this is a correct approach.

    $str = "auth_plugin_stack = a,b,EXT::USCC::USCCAuth,c,d"; print "$str \n"; $str =~ s/((\w+::)+\w+,)//; print "$str \n";
    answer:
    auth_plugin_stack = a,b,EXT::USCC::USCCAuth,c,d auth_plugin_stack = a,b,c,d

Re: regex to remove a word from string
by Laurent_R (Canon) on Dec 04, 2014 at 07:51 UTC
    Your question is far too general. The answer could very well be davido's solution if your intent is only to remove the litteral unwanted string, or it could be more complex if what you want to remove can take other values. Si you should decribe as precisely as possible the characteristics of what you want to remove (and/or what you want to keep) from the original string. A single example is almost never sufficient to describe what a regex should do, you need to give clear rules to explain what needs to ke kept and what should be remoived.
Re: regex to remove a word from string
by upaksh (Novice) on Dec 04, 2014 at 09:15 UTC
    Got the regex.
    $str =~ s/EXT::USCC::USCCAuth,?|,?EXT::USCC::USCCAuth//;
    Thanks all.
Re: regex to remove a word from string
by Anonymous Monk on Dec 04, 2014 at 17:55 UTC
    By far the easiest way to handle a requirement like this is to split() the string into an array, then use “splicing” to remove the element of the array (reorder it, etc...), then join() the array back together to form a new string. This is a generalized solution to the problem that can easily survive the inevitable changes and refinements, without putting anyone through “regex hell.”