Regular Expression Not Working in Condition

Hi all,

I am trying to use a condition to catch the case in which the query string of a request contains two or more parameters from a specific list. In such a case I wish to raise an error.

Of course, I can use many "and" and "or" clauses, but that will get very messy very quickly as the size of the list of parameters increases. So instead, I opted to use a regex to test for this.

As an example, if the list of parameters is [Bird,Dog,Horse], then any request who has two or more of these parameters in its query string should be matched.

The regular expression I am using is:

 /(.*(Bird|Dog|Horse).*){2} 

I tested in various regex testers and it works.

However, when I put the condition:

<Condition>request.querystring Matches "/(.*(Bird|Dog|Horse).*){2}"</Condition> 

I never get a match.

Am I missing some specific APIGEE regex rules? Maybe the "{2}" is not supported in APIGEE? Thank you very much!!

Adam

Solved Solved
1 7 3,501
1 ACCEPTED SOLUTION

Similar question is answered here .

Use JavaRegex in your condition.

View solution in original post

7 REPLIES 7

Similar question is answered here .

Use JavaRegex in your condition.

Yes. And ... for more information, check the reference doc for Conditions.

If the current doc has incorrectly led you to believe that the Matches operator accepts a regex, let me know, and we'll get it corrected.

There was a time, years ago, during which I found a couple mentions of "regular expression" in the documentation when discussing the "Matches" operator. I think we caught and removed all of those incorrect usages in the doc.

I did try using "JavaRegex" before instead of "Matches", but it didn't work.

I investigated the Java code itself and found out the if you use "JavaRegex" you don't need to put the "/" at the beginning of your pattern.

The documentation does put a "/" at the beginning when using JavaRegex, which is why it did not work for me at the beginning (https://docs.apigee.com/api-platform/fundamentals/flow-variables-and-conditions#javaregex)

Thanks!

@jonesfloyd - This is worth investigating.

Thanks, @Dino-at-Google. b/123756861

In the example above, the conditional statement is matching a query string:

<Condition>request.querystring Matches "/(.*(Bird|Dog|Horse).*){2}"</Condition>


This would only match if the query string starts with "/" -- which it probably doesn't.

On the other hand, the doc examples are matching a proxy.pathsuffix, a string that always starts with "/". I think this is the source of confusion --- the doc isn't saying you have to use a "/" with JavaRegex. For example, the following condition works as expected, because it's matching the pathsuffix "/cat", not "cat":

<Condition>(proxy.pathsuffix JavaRegex "/cat")</Condition>

I think this is the source of confusion and will update the doc with other types of examples. Also there are other examples in the doc that illustrate this.


Happy to stand corrected and investigate further if I'm missing something.

Thank you Will !! Good catch.