Forum Discussion

Oferb's avatar
Oferb
Icon for Nimbostratus rankNimbostratus
Feb 09, 2017

Irule with Dynamic Regexp

Hello,
I need help about Regexp.
The [HTTP::payload] at HTTP_REQUEST_DATA is dynamic. i made a procedure that build the regexp.
i don't know how to write the "if" Sentence.

The procedure:

set RegexStr "\\\["
set Body [HTTP::payload]  ->>>[210,21,33]
set Body [string range $Body 1 end-1]
set Body [string map {"," " "} $Body]
set ItemCount [llength $Body]
for {set x 0} {$x < $ItemCount} {incr x} {
    set Item [lindex $Body $x]
    set ItemLength [string length $Item]
    if {$x < $ItemCount} {
        append RegexStr "(\\d{$ItemLength}),"
        }
    else {
        append RegexStr "(\\d{$ItemLength})\\\]"
    }
}

The Result of this procedure: $RegexStr = "[(d{3}),(\d{2}),(\d{2})]" ---->match the Body

I need solution like this "IF":

if {$Body matches_regex $RegexStr} { log local0. "Matched" }

Thanks

6 Replies

  • Hi Oferb,

     

    can you please explain the outcome of this function? I didn't get the point of building a RegEx pattern based on certain input data followed by a check if the very same input data matches the created RegEx pattern.

     

    Note: The question seems to be a carbon copy of https://devcentral.f5.com/questions/regexp-as-string-dynmic-51588. Same user or same problem/requirement?

     

    Cheers, Kai

     

  • Hi Kai, Thank you for your response.

     

    I will explain, I Recieve this kind of data in the request body;

     

    [10] or [43,11] OR [55,65,999]

     

    And so on.

     

    I need to write a regexp that will match this inputs.

     

    Thanks again

     

  • Hi Shuki,

     

    This doesn't explain the entire outcome. Once you build the RegEx, what is the purpose of having such pattern?

     

    The only reson I see right now, is to check if each segment (while ignoring the total number of segments) of the message contains only digits (while ignoring the total digit lenght), isn't it?

     

    Cheers, Kai

     

  • Clarity would be helpful. Once you provide clear guidance on what you are trying to accomplish, I'd recommend you also take a look at scan instead of regex. Just some pointers to give you additional ideas:

    % set opts [list "\[10\]" "\[43,11\]" "\[55,65,999\]"]
    {[10]} {[43,11]} {[55,65,999]}
    % foreach pattern $opts {
      set num [scan [string trim [string trim $pattern "\["] "\]"] %d,%d,%d x y z]
      switch $num {
      1 { puts Result: \n $x }
      2 { puts "$x, $y" }
      3 { puts "$x, $y, $z" }
      }
    }
     Result:
     10
     43, 11
     55, 65, 999