我知道,除了扫描预定义的原始
类型之外,我还可以扫描我自己的用户定义模式,这在扫描更复杂的数据时很有帮助。我说的是 Scanner.next(String pattern)
方法。
然而,在我读到的关于 Java 的书中,有一段说
"There’s one caveat when scanning with regular expressions. The pattern is matched against the next input token only, so if your pattern contains a delimiter it will never be matched."
我不太明白这意味着什么以及这种类型的扫描在哪些情况下不适用
最佳答案
假设您有一个逗号 ,
作为分隔符。现在,不知何故(也许是由其他人提供的)您已经想出了一个模式 ab,cd
。由于模式包含分隔符,扫描器将尝试首先将 ab,cd
与 ab
匹配,然后与 cd
匹配,导致不匹配。
请注意,这是一个警告,而且您不一定会轻易遇到。
关于java - Java中使用正则表达式扫描有什么缺点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38433780/