http://www.perlmonks.org?node_id=184214

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

Hello monks ,,, how can I subtract or actually clean the end of a word.. for example if i have this variable
$directory = home] ;
so I want to get rid of the "]" at the end of home so it should look like this :
$directory = home;
thanks :)

Replies are listed 'Best First'.
Re: the end of a word
by arturo (Vicar) on Jul 22, 2002 at 20:22 UTC

    A lot depends on what you want to "clean." For the specific case you mention (with all those naughty barewords... put on some quotes, or you'll catch a horrible illness), you might be able to employ the builtin character class \w ("word" characters) or its negation, \W (not "word" characters). The perldoc perlre can help a lot here. Since I can't tell exactly what you're trying to fix, I'll assume you want to get rid of any non-word characters at the end of the string, for which

    $directory =~ s/\W+$//;
    ought to suffice. The $ anchors the match to the end of the string, and "\W+" matches "one or more non-word characters". It then replaces any such pattern found with nothing.

    HTH

    I mistrust all systematizers and avoid them. The will to a system shows a lack of integrity -- F. Nietzsche

      thank you all
Re: the end of a word
by Ionitor (Scribe) on Jul 22, 2002 at 20:24 UTC
    Well, if all you want to do is remove the last character of a string, regardless of what it is, then:
    chop $directory;
    will work. However, more is needed if you're looking to eliminate an indetermined amount of junk. A general regex to customize would be:
    $directory =~ s/[^a-zA-Z]+$//;
    This will remove any non-letters from the end of the variable (the ^ negates the [] character class). If there are other valid word letters, change the character class as needed.

      [^a-z-A-Z] can be more succintly put as \W . (That's a capital W in case your font doesn't make it clear.)

      --
      જલધર

        That's a common misconception regarding \w and \W. The "word" characters (depending upon locale) are typically letters, underscores, and digits. Thus, the \W will not remove underscores or digits from the end.

        $ perl -e '$_="foobar_";s/[^a-zA-Z]+$//;print' foobar $ perl -e '$_="foobar_";s/\W+$//;print' foobar_ $ perl -e '$_="foobar9";s/\W+$//;print' foobar9

        Update: I pointed out a bug in your code only to realize a bug in mine :) My first example doesn't respect locale settings. I should be using POSIX character classes.

        $ perl -e '$_="foobar_";s/[[:^alpha:]]+$//;print' foobar

        Cheers,
        Ovid

        Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: the end of a word
by Ovid (Cardinal) on Jul 22, 2002 at 20:28 UTC

    I'm afraid you'll have to provide a better explanation for "subtract or actually clean the end of a word". One solution (which may not fit, depending upon your needs), would be to use a word boundary (\b) with a regex.

    $ perl -e '($_)="foobar]"=~/(f.*?)\b/;print' foobar

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: the end of a word
by ichimunki (Priest) on Jul 22, 2002 at 22:57 UTC
    perldoc -f chop

    for when you know you don't want the last letter

    perldoc -f tr

    for when you want to get rid of certain characters no matter where they appear, example: $directory =~ tr/\]//

    perldoc perlre

    for when you need to do some fancy reg ex substitution stuff to get at real specific stuff like remove a specific character at the end of a string, example: $directory =~ s/\]$//

Re: the end of a word
by dbalagi2 (Initiate) on Jul 23, 2002 at 13:40 UTC
    $directory =~ s/. \t*\;$/ \;/; the space and the tabs need to be enclosed in square braces . not able to get the formatting rigth