regex 正则表达式字符串匹配uuid beetwin单词[已关闭]

cig3rfwq  于 2023-10-22  发布在  其他
关注(0)|答案(2)|浏览(119)

**已关闭。**此问题需要debugging details。它目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
上个月关门了。
Improve this question
如何编写匹配此字符串的正则表达式
已处理接收到的请求,其messageId:test,clientId:7 da 6554 c-e3 ed-49 ed-9a 7 f-f810 f98 ceb 7 f
UUID将始终不同
所以我想检查一些大的文本,如果它包含
已处理接收到的messageId:test,clientId:some uuid 的请求

bigText.matches(regex) == true
ffdz8vbo

ffdz8vbo1#

可以使用此正则表达式匹配指定的字符串模式

Received request with messageId:test, clientId:[\\w-]+ has been processed

Java语言的完整代码:

import java.util.regex.*;

public class Main {
    public static void main(String[] args) {
        // Your larger text
        String largerText = "Received request with messageId:test, clientId:7da6554c-e3ed-49ed-9a7f-f810f98ceb7f has been processed";

        // Define the regular expression pattern
        String pattern = "Received request with messageId:test, clientId:[\\w-]+ has been processed";

        // Create a Pattern object
        Pattern regex = Pattern.compile(pattern);

        // Create a Matcher object
        Matcher matcher = regex.matcher(largerText);

        // Check if a match was found
        if (matcher.find()) {
            String matchedText = matcher.group();
            System.out.println("Match found: " + matchedText);
        } else {
            System.out.println("No match found");
        }
    }
}

输出:

Match found: Received request with messageId:test, clientId:7da6554c-e3ed-49ed-9a7f-f810f98ceb7f has been processed

说明:
**pattern:**是正则表达式模式,用于查找UUID的固定部分“Received request with messageId:test,clientId:“,后跟一个或多个单词字符(\w)和连字符(-)。

dxpyg8gm

dxpyg8gm2#

我不使用正则表达式,而只查找消息前缀。

String message = "Some other text here.  Received request with messageId:test, clientId:7da6554c-e3ed-49ed-9a7f-f810f98ceb7f has been processed";

System.out.println(checkRequest(message) ? "Received" : "Unknown request");
         
        
static String MESSAGE_PREFIX = "Received request with messageId:test, clientId:";
public static boolean checkRequest(String text) {
    return text.indexOf(MESSAGE_PREFIX) >= 0;
}

相关问题