简体   繁体   English

如何对Java扫描器使用多个定界符

[英]How to use multiple delimiters for a java scanner

I am tryng to scan through an assembly file where the instruction and registers are separated by white space and commas. 我正在尝试浏览汇编文件,其中的指令和寄存器用空格和逗号分隔。 My delimiter is: 我的分隔符是:

s.useDelimiter("\\s|\\,\\|\\(|\\)");

where I am trying to delimit whitespaces, commas, as well as open and close parentheses. 在这里我试图划定空格,逗号,以及打开和关闭括号。

The input is: 输入为:

addi $t0, $t0, 100
addi $a0, $a0,100

However even while scanning through I end up with test outputs like: 但是即使进行扫描,我最终也会得到如下测试输出:

addi
$t0,
$t0,
100
addi
$a0,
$a0,100

It wont separate by commas for some reason. 由于某种原因,它不会以逗号分隔。 My desired output is: 我想要的输出是:

addi
$t0
$t0
100
addi
$a0
$a0
100

I greatly appreciate your help! 非常感谢您的帮助!

Follow up for Samarth 跟进萨玛斯

sw$a0,4($a1) sw $ a0,4($ a1)

should be: 应该:

sw
a0
4
a1

I have now found that I also need to delim by dollar signs, so for this example what should the delimiter be? 现在,我发现我还需要按美元符号进行下限,因此对于此示例,定界符应为什么?

I tried using s.useDelimiter("\\\\s?\\\\$|\\\\s?,\\\\s?\\\\$?|\\\\s|\\\\(\\\\s?\\\\$?|\\\\s?\\\\)"); 我尝试使用s.useDelimiter("\\\\s?\\\\$|\\\\s?,\\\\s?\\\\$?|\\\\s|\\\\(\\\\s?\\\\$?|\\\\s?\\\\)"); with this pattern I got output as 使用这种模式,我得到的输出为

addi 阿迪
t0 00
t0 00
100 100
addi 阿迪
a0 00
a0 00
100 100

Hey if this is what you are looking for? 嘿,这是您要找的东西吗? If not please share any cases where you are not getting result as expected. 如果没有,请分享您没有得到预期结果的任何情况。

Find sample code here at https://ideone.com/0125ZV https://ideone.com/0125ZV上找到示例代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM