I am trying to come up with a regular expression that gets rid of all the punctuations(if there is one or more) both at the top and the end of a string. The regex I am using now looks like this:(word is the string I want to convert)
word = word.replaceAll("['?:!.,;]*([a-z]+)['?:!.,;]*", "$1").toLowerCase();
However, I still get some weird cases. For example, 'Amen'
goes to 'amen'
and ''tis
goes to 'tis
. Can anyone help me modify it so that 'Amen'
will go to amen
and ''tis
to tis
. Thanks in advance!
Replace the following pattern:
^\p{P}+|\p{P}+$
With an empty string.
\p{P}
means any punctuation character. The first part of the regex will remove punctuation at the start, and the second will do it at the end.