简体   繁体   中英

Pattern matching using Regex in Java

I have a stream from which I read a string that looks like the following:

event.tag.report tag_id=0xABCD0029605, type=ISOB_80K, antenna=1, frequency=918250, rssi=-471, tx_power=330, time=2017-12-18T19:44:07.198
                        ^^^^^^^^^^^^^

I am trying to use Regex to just get the highlighted part (underlined by ^^^^ ) for every string that I read. My pattern for the Regex is as follows:

.*\\s(tag_id=)(.{38})(\\,\\s)(.*)$

However, this does not work for tag_id s which are longer than or shorter than 38 digits.

Can someone help me with a string pattern that will help me just get the highlighted area in the string independent of its size?

Looks to me as though you want all hexidecimal characters:

"tag_id=(0x[A-F0-9]+)"

So

Pattern pattern = Pattern.compile("tag_id=(0x[A-F0-9]+)");
Matcher matcher = pattern.matcher("event.tag.report tag_id=0x313532384D3135374333343435393031, type=ISOC");
if (matcher.find())
    System.out.println(matcher.group(1));

returns:

0x313532384D3135374333343435393031

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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