Constructing regex pattern to match sentence
I'm trying to write a regex pattern that will match any sentence that begins with multiple or one tab and/or whitespace. For example, I want my regex pattern to be able to match " hello there I like regex!" but so I'm scratching my head on how to match words after "hello". So far I have this:
String REGEX = "(?s)(\\p{Blank}+)([a-z][ ])*";
Pattern PATTERN = Pattern.compile(REGEX);
Matcher m = PATTERN.matcher(" asdsada adf adfah.");
if (m.matches()) {
System.out.println("hurray!");
}
Any help would be appreciated. Thanks.
An example regex to match sentences by the definition: "A sentence is a series of characters, starting with at lease one whitespace character, that ends in one of .
, !
or ?
" is as follows:
\s+[^.!?]*[.!?]
Note that newline characters will also be included in this match.
String regex = "^\\s+[A-Za-z,;'\"\\s]+[.?!]$"
^
means "begins with"\\s
means white space+
means 1 or more[A-Za-z,;'"\\s]
means any letter, ,
, ;
, '
, "
, or whitespace character$
means "ends with"
If you looking to match all strings starting with a white space you can try using "^\s+*" regular expression.
This tool could help you to test your regular expression efficiently.
http://www.rubular.com/