0

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_ids 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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Hey, this seems to work only partially I am unable to get the entire string before the ,. if my String has tag_id=0x313532384D3135374333343435393031 then it only outputs tag_id=0x313532384D313537.
@SagarNair Not for me. I added my test code and it works fine. Are you sure you don't have a hidden character after the 7?
can you please try this string event.tag.report tag_id=0x3504B40180019F112A05F6EB, type=ISOC, antenna=2, frequency=918250, rssi=-638, tx_power=330, time=2017-12-18T20:17:02.865. I am sorry I know I can too but its not working for me. Thanks :)
@SagarNair yes that works fine. Altered the code to demonstrate. Note that I changed matches for find because the pattern matches a part of the input, not the entire input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.