So, I'm completely unaccustomed to regular expressions, and I'm trying to use Java java.util.regex to search for punctuation in input strings. I will not know what punctuation I could get ahead of time, except that (1)!,?,., ... all valid puncutation and (2) "<" and ">" mean that something special and not considered punctuation. The program itself constructs phrases pseudo-randomly, and I want to cancel punctuation at the end of a sentence before it goes through a random process.
I can match whole words with any punctuation, but coincidence just gives me indexes for that word. In other words:
Pattern p = Pattern.compile("(.*\\!)*?"); Matcher m = p.matcher([some input string]);
will capture any words with !! in the end. For example:
String inputString = "It is a warm Summer day!"; Pattern p = Pattern.compile("(.*\\!)*?"); Matcher m = p.matcher(inputString); String match = inputString.substring(m.start(), m.end());
leads to → String match ~ "day!"
But I want the Matcher index just “!”, So I can just separate it.
Perhaps I could do cases and use String.substring (...) for every kind of punctuation I could get, but I hope that I am mistaken in using regular expressions for this.
Mister r2
source share