Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

java - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...