简体   繁体   English

正则表达式检查字符串是否匹配特定模式

[英]Regular expression to check if a string match certain pattern

How to use Java regular expression to check if a string follows a certain pattern ?如何使用 Java 正则表达式检查字符串是否遵循特定模式? eg to check if a message first starts with " In the morning ", then followed by any words, then followed by " In the afternoon ", and then followed by any words.例如,检查消息是否首先以“ In the morning ”开头,然后是任何单词,然后是“ In the afternoon ”,然后是任何单词。

I have tried to look up the regex syntax but found it difficult to understand.我试图查找正则表达式语法,但发现它很难理解。

I have tried to use |我曾尝试使用| , but this is an "Or" operator. ,但这是一个“或”运算符。 And it does not specify the ordering of first matching " In the morning " and then " In the afternoon ".并且没有指定先匹配“ In the morning ”再匹配“ In the afternoon ”的顺序。

Pattern pattern = Pattern.compile("\\bIn the morning\\b|\\bIn the afternoon\\b");
Matcher matcher = pattern.matcher("In the morning I read the news, then I start my work. In the afternoon I have my lunch.");

^In the morning .*In the afternoon.*

The ^ matches the start of the expression to match. ^ 匹配要匹配的表达式的开头。 The .* matches zero or more non return characters. .* 匹配零个或多个非返回字符。

You can also put parenthesis around the .* to form capture groups to find out what actually you did in the morning and afternoon您还可以在 .* 周围加上括号以形成捕获组,以找出您在上午和下午实际执行的操作

^In the morning (.*)In the afternoon (.*)

regex101正则表达式101

String str = "In the morning I read the news, then I start my work. In the afternoon I have my lunch.";
Pattern pattern = Pattern.compile("^\\bIn the morning\\b.*\\bIn the afternoon\\b.*$");
Matcher matcher = pattern.matcher(str);

if (matcher.matches())
    System.out.println("match");
else
    System.err.println("not match");
  • ^ asserts position at the start of a line ^断言行首的位置
  • \\b assert position at a word boundary: (^\\w|\\w$|\\W\\w|\\w\\W) \\b在单词边界处断言位置: (^\\w|\\w$|\\W\\w|\\w\\W)
  • . matches any character (except for line terminators)匹配任何字符(行终止符除外)
  • * matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy) *匹配前一个令牌在零次和无限次之间,尽可能多次,根据需要回馈(贪婪)
  • $ asserts position at the end of a line $在行尾断言位置

I think you want .* in between the two phrases, rather than an alternation.我认为您希望.*介于两个短语之间,而不是交替使用。 Try this version:试试这个版本:

Pattern pattern = Pattern.compile("\\bIn the morning\\b.*\\bIn the afternoon\\b");
Matcher matcher = pattern.matcher("In the morning I read the news, then I start my work. In the afternoon I have my lunch.");
if (matcher.find()) {
    System.out.println(matcher.group());
}

This prints:这打印:

In the morning I read the news, then I start my work. In the afternoon

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM