Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove all punctuation that follows a string?

It's for a game in which the user can input a value like "Iced tea.." I would like to manipulate the string to return "Iced tea" without the trailing punctuation marks.

Looking for most elegant / simplest python solution.

Tried

def last_character(word):
  if word.endswith('.' or ','):
      word = word[:-1]
  return word 

which works if there's only one punctuation mark at the end. But it's not all-encompassing.

Found a Java solution:

String resultString = subjectString.replaceAll("([a-z]+)[?:!.,;]*", "$1");
like image 331
user1186742 Avatar asked May 03 '12 03:05

user1186742


People also ask

How do I get rid of punctuation in regular expressions?

You can use this: Regex. Replace("This is a test string, with lots of: punctuations; in it?!.", @"[^\w\s]", "");

How do you get rid of all punctuation in a string JavaScript?

We can use the JavaScript string replace method with a regex that matches the patterns in a string that we want to replace. So we can use it to remove punctuation by matching the punctuation and replacing them all with empty strings.

How do you strip punctuation from a string in Python?

Use regex to Strip Punctuation From a String in Python The regex pattern [^\w\s] captures everything which is not a word or whitespace(i.e. the punctuations) and replaces it with an empty string.


1 Answers

>>> 'words!?.,;:'.rstrip('?:!.,;')
'words'
like image 149
Ignacio Vazquez-Abrams Avatar answered Sep 24 '22 15:09

Ignacio Vazquez-Abrams