Reputation: 13
Lets say I have a string
String line = "Let x0 be {1,2,3,4}"
How can I check if it the String is written in "Let ??? be ???"
I was trying with the code below but I thing you need to know the length of the ??? to make it work
matches = line.matches("Let \\d be \\d")
Thank you
Upvotes: 1
Views: 314
Reputation: 1230
\d
only match digit, short for [0-9], it can't match x0
, neither any char of "{,}"
so you can match with \S
, which means a non-whitespace character. and since you do not know the length, and expect at least one, can add "+
" to \S, like \S+
.
so this one can match your expect: Let \S+ be .*
Upvotes: 2
Reputation: 4714
try this regex "Let (\w+) be (\S+)"
String line = "Let x0 be {1,2,3,4}";
Pattern p = Pattern.compile("Let (\w+) be (\S+)");
Matcher m = p.matcher(line);
int lastMatchPos = 0;
while (m.find()) {
System.out.println(m.group(1));
System.out.println(m.group(2));
if(m.group(2).length> m.group(1).length){
//do something you want
}
}
Upvotes: 1