Building a regular expression pattern to match an expression

I am trying to write a regex pattern that matches any sentence that starts with several or one tab and / or space. For example, I want my regex pattern to match "hello, I like regex!". but I scratch my head how to combine words after "hello". So far I have this:

String REGEX = "(?s)(\\p{Blank}+)([az][ ])*"; Pattern PATTERN = Pattern.compile(REGEX); Matcher m = PATTERN.matcher(" asdsada adf adfah."); if (m.matches()) { System.out.println("hurray!"); } 

Any help would be greatly appreciated. Thanks.

+6
source share
4 answers
 String regex = "^\\s+[A-Za-z,;'\"\\s]+[.?!]$" 

^ means "starts with"
\\s means space
+ means 1 or more
[A-Za-z,;'"\\s] means any letter [A-Za-z,;'"\\s] ; , ' , " or a space character
$ means "ends" with

+15
source

An example of a regular expression to match sentences by definition: “A sentence is a series of characters, starting with leasing one whitespace that ends with one of . , ! Or ? ” As follows

 \s+[^.!?]*[.!?] 

Regular expression visualization

Note that newlines will also be included in this match.

+11
source

Based on what you desire and ask, the following will work.

 String s = " hello there I like regex!"; Pattern p = Pattern.compile("^\\s+[a-zA-Z\\s]+[.?!]$"); Matcher m = p.matcher(s); if (m.matches()) { System.out.println("hurray!"); } 

See working demo

+1
source

If you want to combine all lines starting with a space, you can try using the "^ \ s + *" regular expression.

This tool will help you check your regular expression effectively.

http://www.rubular.com/

+1
source

Source: https://habr.com/ru/post/959142/


All Articles