Issue
"--> xxx", where xxx is a digit
example:
--> 200
--> 201
--> 403
I want to check if a string begins with that pattern
My attempt:
message.contains(Regex("-->\\s+(\\d+)"))
Solution
In Java String
you'd use the regex you have plus ".*" to match other characters after digits with matches(regex)
:
message.matches("-->\\s+(\\d+).*")
So these are all true:
"--> 200 xyz".matches("-->\\s+(\\d+).*")
"--> 403".matches("-->\\s+(\\d+).*")
If repeating this often you may be better off creating a Pattern
and Matcher
once before resetting the matcher instance each time for a string to check:
Pattern p = Pattern.compile("-->\\s+(\\d+).*");
Matcher m = p.matcher("");
// Following are all true, showing string starts with "--> NNN":
m.reset("--> 200 abc").matches();
m.reset("--> 403 xyz").matches();
Answered By - DuncG
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.