我正在尝试使用Scanner来分解我从文件中读取的字符串。 文件数据是:
RFH ^@^@^@^B^@^@^@°^@^@^A^Q^@^@^D¸
^@^@^@^@^@^@^D¸^@^@^@
<mcd><Msd>jms_bytes</Msd></mcd>
^@^@^@d<jms><Dst>queue:///panddArchiveVerifyStep1.V001_I</Dst><Tms>1280138410102</Tms><Dlv>2</Dlv>< /jms> 571:8:*SYD01_P,31:*panddArchiveVerifyStep1.V001_I,520:454:28:panddArchiveVerifyStep1.V001,417:<?xml version="1.0" encoding="UTF-8"?> <n0:message xmlns:n0="uri:ebusiness.com"><n0:messageHeader><n0:messageType>panddArchiveVerify</n0:messageType><n0:messageVersion>001</n0:messageVersion></n0:messageHeader><n0:archiveDoc><n0:docImageID>14256448</n0:docImageID><n0:initialDispatchDatetime>2010-06-16T20:40:48.495</n0:initialDispatchDatetime><n0:processCount>0</n0:processCount></n0:archiveDoc></n0:message>,,4:cert,16:dummycertificate,4:algo,3:DES,3:sig,9:[B@7b3082,0:,,,
我需要做的步骤是:
获取:28 到</n0:message>
的长度
将此长度前缀为之前:28并删除其余部分
是否有正则表达式我可以调用以获取字符串标记:28到</n0:message>
?
到目前为止,我有一个分隔符来获取字符串令牌:28但我不知道如何停在</n0:message>
。
Scanner s = new Scanner(rawMsg.toString()).useDelimiter("(?=:28)");
Example data
:28:panddArchiveVerifyStep1.V001,417:<?xml version="1.0" encoding="UTF-8"?><n0:message xmlns:n0="uri:ebusiness.com......
我想要的原始数据是三个令牌:
Token One:
RFH ^@^@^@^B^@^@^@°^@^@^A^Q^@^@^D¸
^@^@^@^@^@^@^D¸^@^@^@
<mcd><Msd>jms_bytes</Msd></mcd>
^@^@^@d<jms><Dst>queue:///panddArchiveVerifyStep1.V001_I</Dst><Tms>1280138410102</Tms><Dlv>2</Dlv>< /jms>
571:8:*SYD01_P,31:*panddArchiveVerifyStep1.V001_I,520:454
Token two:
:28:panddArchiveVerifyStep1.V001,417:<?xml
version="1.0" encoding="UTF-8"?>
<n0:message
xmlns:n0="uri:ebusiness.asic.gov.au"><n0:messageHeader><n0:messageType>panddArchiveVerify</n0:messageType><n0:messageVersion>001</n0:messageVersion></n0:messageHeader><n0:archiveDoc><
n0:docImageID>14256448</n0:docImageID><n0:initialDispatchDatetime>2010-06-16T20:40:48.495</n0:initialDispatchDatetime><n0:processCount>0</n0:processCount></n0:archiveDoc></n0:message>
Token three:
,,4:cert,16:
dummycertificate,4:algo,3:DES,3:sig,9:[B@7b3082,0:,,,
答案 0 :(得分:1)
你走在正确的轨道上;只需使用后面的作为下一个分隔符:
if ( sc.useDelimiter("(?=:28)").hasNext() )
{
System.out.printf("%n%s%n", sc.next() );
}
if ( sc.useDelimiter("(?<=</n0:message>)").hasNext() )
{
System.out.printf("%n%s%n", sc.next() );
}
if ( sc.useDelimiter("\\z").hasNext() )
{
System.out.printf("%n%s%n", sc.next() );
}
但是,如果您已经将文本读入字符串,则可能更容易使用Matcher.find()
或String.split()
,甚至indexOf()
和substring()
。如果你有兴趣,我会详细说明。