Why does this regex not match in my camel route?

0

I want to route messages based on content, however my regex for doing so doesn't match what I throw at it. Why is that so?

I've tried some online regex test websites and on these websites, I get a full match. I've also adapted my regex to ".*\{value=1\}.*", which works as expected, but I think my original solution might be more robust.

This is my route:

<from uri="milo-client:opc.tcp://localhost:12685?allowedSecurityPolicies=None&amp;node=RAW(ns=2;s=items-machine_lfm_reset)&amp;overrideHost=true"/>
<choice>
    <when>
        <simple>${bodyAs(String)} regex "/\bvalue=1\b/"</simple>
        <log message="Value was 1."/>
    </when>
<choice>

This is the message that comes in:

DataValue{value=Variant{value=1}, status=StatusCode{name=Good, value=0x00000000, quality=good}, sourceTime=DateTime{utcTime=132061080449440000, javaDate=Thu Jun 27 11:20:44 GMT 2019}, serverTime=DateTime{utcTime=132061080459370000, javaDate=Thu Jun 27 11:20:45 GMT 2019}}

I expected this regex to match, because if I enter my message and regex on any regex testing website, I get a match, but actually I don't get a match and my when statement isn't fulfilled.

regex
xml
apache-camel
asked on Stack Overflow Jun 27, 2019 by Necrophades • edited Jun 27, 2019 by Wiktor Stribiżew

1 Answer

0

Your input actually does not match this regex. Simple function regex uses Matcher#matches() method, which returns true only for full match.

JDoodle

If you have seen this regex to match in some tester website, then it is probably using Matcher#find(), which returns true even for partial match.

https://www.regexplanet.com/share/index.html?share=yyyydc68p6r

See Matcher#matches javadoc:

Returns: true if, and only if, the entire region sequence matches this matcher's pattern

answered on Stack Overflow Jun 27, 2019 by Bedla • edited Jun 27, 2019 by Bedla

User contributions licensed under CC BY-SA 3.0