Question

Iam trying to come up with a camel route, where a message will be sent to a jms queue only it if matches a given regex expression..

The route that I have is this:

<route id="testRoute">
    <from uri="jms:queue:Q.Order1" />
       <choice>
          <when>
             <simple>${body} regex '\w+.*'</simple>
             <to uri="jms:queue:Q.Order2"/>                             
          </when>
       </choice>        
</route>

If my msg. is this, it passes the regex just fine:

000000010020140507

However, if my msg is this, it fails:

00000001002     REXRYAN 004                                                           
00000002076006490993999900000

Why is the second message failing regex and what needs to be modified in the regex to pass the second message? Thanks

Était-ce utile?

La solution

In regex, . by default does not match newlines in most languages. In your situation, you have a multilined string and the body thus don't match the regex \w+.* since only the first line matches.

There are usually two ways to by-pass this:

  1. Use a modifier/flag which makes . match newlines as well (but I don't know Camel, so I can't be sure how to do this, though adding (?s) at the beginning of the regex might make this work just as well since that's the equivalent of setting that particular modifier/flag).

  2. Use a class containing both a character and its negation.

    For example, [\s\S] contains \s (whitespace character which includes newlines) and \S which matches non-whitespace characters. Together, they will match everything, because \S will match everything \s doesn't match.

    You could have used [\w\W] just as well to give the same result, or [\d\D] or similar constructs.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top