Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: the end of a word

by Ionitor (Scribe)
on Jul 22, 2002 at 20:24 UTC ( [id://184221]=note: print w/replies, xml ) Need Help??


in reply to the end of a word

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.

Replies are listed 'Best First'.
Laziness is one of the three virtues!
by jaldhar (Vicar) on Jul 22, 2002 at 20:31 UTC

    [^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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://184221]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-24 11:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found