JavaRegex not working in Apigee

I am trying to use Regular Expression in a Condition in Apigee, to match on strings that contain non-word characters. This is the relevant code.

<Step>
  <Name>FC-InvalidScope</Name>
  <Condition>(request.queryparam.scope JavaRegex "\W")
          or (request.queryparam.scope JavaRegex "(\W)")</Condition>
</Step> 

In the trace I could see the value of request.queryparam.scope = payment!!@$%^*(.

In other words, the scope query param contains non-word characters. In that case, I expected the condition to evaluate to true. But apparently not, this Policy is still being skipped.

Why?

How can I get a condition that evaluates to true when the query param contains any non-word characters?

Solved Solved
0 3 479
1 ACCEPTED SOLUTION

The JavaRegex condition matches the entire left-hand-side.

Which means your condition will evaluate to true only when the query param consists of exactly one non-word character.

BTW, I am not sure if this behavior of the javaregex condition is clear in the documentation or not. If it is not, then we should update the doc. Let me know.

I think what you are trying to do is get a TRUE condition when the queryparam contains *any* non-word characters. In that case, you can negate the match on *all* word characters.

like this

<!-- condition is true when the param is not all word chars -->
<Condition>NOT (request.queryparam.scope ~~ "\w+")</Condition>

When the queryparam is "payment" then the condition is not true.

When the queryparam is "payment?!?@?#?$$?" then the condition evaluates to true.

Or, you could do similarly with lazy wildcards:

<!-- condition is true when the param has any non-word chars -->
<Condition>request.queryparam.scope ~~ ".*?\W.*?"</Condition>

View solution in original post

3 REPLIES 3

The JavaRegex condition matches the entire left-hand-side.

Which means your condition will evaluate to true only when the query param consists of exactly one non-word character.

BTW, I am not sure if this behavior of the javaregex condition is clear in the documentation or not. If it is not, then we should update the doc. Let me know.

I think what you are trying to do is get a TRUE condition when the queryparam contains *any* non-word characters. In that case, you can negate the match on *all* word characters.

like this

<!-- condition is true when the param is not all word chars -->
<Condition>NOT (request.queryparam.scope ~~ "\w+")</Condition>

When the queryparam is "payment" then the condition is not true.

When the queryparam is "payment?!?@?#?$$?" then the condition evaluates to true.

Or, you could do similarly with lazy wildcards:

<!-- condition is true when the param has any non-word chars -->
<Condition>request.queryparam.scope ~~ ".*?\W.*?"</Condition>

Hi, might I ask,

If I have the ff. condition,

(request.queryparam.scope ~~ "\w+")

this returns a FALSE value if I have a value of pay!@$ment then. Isn't it supposed to be a true?. I was thinking if I have this, it would already return a true value and don't have to negate it.

It's just that when I tested this code, it does the exact opposite of what I thought it would as I expect it to return a TRUE value when it found a non-word character in the scope param.

Maybe you need "^\w+$" ?