Friday, July 31, 2009

In perl regex, how do you search for just the last letters of a word?

I have a text file that I would like to search and replace leaving only the first letter of words and punctuation remaining. I don't want to delete words like "a" and "I" either. What regex should I use? (I have a text editor that uses regex to search/replace.)

In perl regex, how do you search for just the last letters of a word?
sofarsogood is wrong. You don't need to escape the parens.


You also need a /g flag in the end





Try something like this instead


s/(\w)\w*/g;





should do what you want.


except, it keeps not only punctuation, but anything, that's not a letter, digit or underscore. If you truly mean to preserve only punctuation, that the easiest would be to do a second pass with





s/[^\w .,;?!: ]//g;





it will strip all the "extra" symbols left over from the first substitution.
Reply:I haven't used perl in a while, but for other regexp I would use something like





s/\([a-z]\)[a-z]+/$1/i





This saves the first alpha char in register 1, and then when it preceeds a nonempty string of chars replaces the whole thing with the first char.





But my suggestion: use emacs!


No comments:

Post a Comment