Layer7 API Management

  • 1.  How do I strip characters from a context variable

    Posted Oct 18, 2018 11:48 AM

    So, I'm trying to remove leading and trailing quotes from a context variable, if they exist (they may or may not with my data).

     

    So I created a regular expression assertion, set both the source and destination context variable to the same value (since I just want to overwrite it).  I selected the "Match and Replace" option under destination, and then tried a bunch of different regexes in the regular expression, using the TEST tab to show me if it was working as desired.   I've found several that work FINE in the test, but spit out the exact wrong thing when running as part of the policy.

     

    So, for example:

     

    "ABC, DEF"  should be rewritten to ABC, DEF

    'ABC DEF' should similarly be rewritten as ABC DEF

    Apostrophes in the middle should be fine, like "ABC's Widgets" should return ABC's Widgets

     

    What am I missing?  Why dont these simple regexs work?

     

    For example, a regex of:

     

    (\")

     

    Strips out the quotes in the test tab, but shows ONLY the quotes when run in a policy.



  • 2.  Re: How do I strip characters from a context variable

    Broadcom Employee
    Posted Oct 18, 2018 04:13 PM

    Hi Richard,

     

    I believe this is because the assertion acts against the source, ie: $(request.mainpart}. If you are outputting the variable from the destination this will capture the match as you are seeing. Try to log ${Request.mainpart} after the RegEx

     

    Regards,

    Joe



  • 3.  Re: How do I strip characters from a context variable

    Broadcom Employee
    Posted Dec 10, 2018 05:58 PM

    Hi Richard

    I am not sure the (\") , is the right match pattern

    .   

    The ( ) is the capture group and \" is the will only capture the " characters, the highlighted areas below.

     

    So it only matches the two " characters, and not the in between stuff. 

     

    Fiddling around I got : 

     

    But you probably want something that matches start and end quotes. 

     

    The following two do work :  ^\"(.*)\"$    and  ^\"(.+)\"$ 

    They match the whole region includign the quotes, but the capture group will be there area between the quotes and not inlcude the quotes.

     

     

     

    They can be a bit tricky. 

     

    Cheers - Mark



  • 4.  Re: How do I strip characters from a context variable

    Broadcom Employee
    Posted Dec 10, 2018 04:42 PM

    Richard,

     

    When you are using the Regular Expression assertion, the value that you set in the lower portion of the window for context variable will store an array created based on the Regular Expression used. It is a good idea to create a separate named one to avoid overwriting the original that you may have used. If you simply just looking to overwrite the value then you can left that field blank.

     

     

    Sincerely,

     

    Stephen Hughes

    Broadcom Support



  • 5.  Re: How do I strip characters from a context variable
    Best Answer

    Broadcom Employee
    Posted Dec 11, 2018 12:44 AM
      |   view attached

    Hi Richard

     

    I've attached the policy for the sample below.  It may not in that current form be exactly what you want but should be close.  

     

     

     

     

    Test cases 

      "boom!"    -> boom!

      'bo'om!'   -> bo'om!

      bo'om!    -> bo'om!

    all worked.

     

    It will also transform  "bo'om!'  ->  bo'om!  Which you may not want.

     

    You can :

    1. use "OR" conditon as per :  regex   ^"(.+)"$|^'(.+)'$
      and then you need to deal with result = ${regexPart[1]}${regexPart[2]}
    2. Or use two different all must be true branches : 
      one of : 
      all must be true
        match : ^"(.+)"$
        result = ${regexPart[1]}
      all must be true 
         match : ^'(.+)'$
         result = ${regexPart[1]}
      all must be true 
         result =${input} 

     

    And attached is cut-and-paste of the policy. 

     

    Best of luck. 

     

    Cheers - Mark

    Attachment(s)

    zip
    policy.xml.zip   684 B 1 version