How to write regex that will be matches to this string
Received request with messageId:test, clientId:7da6554c-e3ed-49ed-9a7f-f810f98ceb7f has been processed
uuid will be always different
so i want to check some big text and if it contains
Received request with messageId:test, clientId:some uuid has been processed
bigText.matches(regex) == true
>Solution :
You can use this regex to match the specified string pattern
Received request with messageId:test, clientId:[\\w-]+ has been processed
Full code for Java language :
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");
}
}
}
Output :
Match found: Received request with messageId:test, clientId:7da6554c-e3ed-49ed-9a7f-f810f98ceb7f has been processed
Explanation:
pattern : is the regular expression pattern that looks for the fixed part "Received request with messageId:test, clientId:" followed by one or more word characters (\w) and hyphens (-) for the UUID.