java - findWithinHorizo​​n 在应该失败时继续读取

标签 java parsing

我有以下代码来交互式地解析 System.in 以获取箭头按键:

Scanner sc = new Scanner(System.in);
String str;
if ((str = sc.findWithinHorizon("\\G\\033\\[C", 0)) != null)
  System.out.println("RIGHT ARROW");
else if ((str = sc.findWithinHorizon("\\G\\033\\[D", 0)) != null)
  System.out.println("LEFT ARROW");

当我第一次按下右箭头时,一切顺利,但是当我第一次按下左箭头时,findWithinHorizo​​n 会挂起等待更多输入,而它应该会失败并返回 null。

通过这种行为,我不知道如何使用这种技术来编写通用模式匹配解析器。

有什么线索吗?

谢谢,弗朗西斯科

最佳答案

findWithinHorizo​​n 等待找到某些内容。你可以尝试一下这个模式

\\G\\033\\[C|\\G\\033\\[D

然后查看字符串内容。

关于java - findWithinHorizo​​n 在应该失败时继续读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4235518/

相关文章:

java - 接受正则表达式并生成 NFA (Java)

java - java.lang.ClassNotFoundException:谷歌应用程序引擎中的sun.security.provider.Sun

java - DDD 实现 - 存储库和工厂

java - 调用弱引用对象的方法

java - Android - 启动与已知设备的蓝牙连接

mysql - BNF规则的解释

c++ - boost spirit 莱克斯和气。集成跳过解析器

Android XML 解析 - URL

java - 通过命令行编译但不是在 ant 中

java - Android应用程序中的JSON解析