Why does this code throw a InputMismatchException ?
Scanner scanner = new Scanner("hello world");
System.out.println(scanner.next("hello\\s*world"));
The same regex matches in http://regexpal.com/ (with \s instead of \\s)
A Scanner, as opposed to a Matcher, has built in tokenization of the string, the default delimiter is white space. So your "hello world" is getting tokenized into "hello" "world" before the match runs. It would be a match if you changed the delimiter before scanning to something not in the string, eg.:
Scanner scanner = new Scanner("hello world");
scanner.useDelimiter(":");
System.out.println(scanner.next("hello\\s*world"));
but it seems like really for your case you should just be using a Matcher
.
This is an example of using a Scanner "as intended":
Scanner scanner = new Scanner("hello,world,goodnight,moon");
scanner.useDelimiter(",");
while (scanner.hasNext()) {
System.out.println(scanner.next("\\w*"));
}
output would be
hello
world
goodnight
moon
The default delimiter of a scanner are whitespaces, so the scanner sees two elements hello and world. And hello\s+world is not matching hello therefore a NoSuchElement exception is thrown.
These inputs work:
"C:\Program Files\Java\jdk1.6.0_21\bin\java" RegexTest hello\s+world "hello world"
'hello world' does match 'hello\s+world'
Here's the code:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest {
public static void main(String[] args) {
if (args.length > 0) {
Pattern pattern = Pattern.compile(args[0]);
for (int i = 1; i < args.length; ++i) {
Matcher matcher = pattern.matcher(args[i]);
System.out.println("'" + args[i] + "' does " + (matcher.matches() ? "" : "not ") + "match '" + args[0] +"'");
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With