Punctuation Regular Expressions

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.

+7
source share
2 answers

I would try a character class regex like

 "[.!?\\-]" 

Add all the characters you want to match inside [] s. Be careful to avoid any characters that may be of particular importance to the regular expression parser.

Then you need to iterate by coincidence using Matcher.find() until it returns false.

+6
source

Java supports POSIX character classes in a workaround. For punctuation, the Java equivalent is [: punct:] - \ p {Punct} .

See more details.

Here is a concrete working example that uses an expression in comments

 import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexFindPunctuation { public static void main(String[] args) { Pattern p = Pattern.compile("\\p{Punct}"); Matcher m = p.matcher("One day! when I was walking. I found your pants? just kidding..."); int count = 0; while (m.find()) { count++; System.out.println("\nMatch number: " + count); System.out.println("start() : " + m.start()); System.out.println("end() : " + m.end()); System.out.println("group() : " + m.group()); } } } 
+19
source

All Articles