简体   繁体   English

在Java中使用Regex进行模式匹配

[英]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. 但是,这对于大于或小于38位的tag_id

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

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

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