Regexp as string - dynmic

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

Hi Shuki,

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.

Cheers, Kai

Duplicate…see here for more details.

Hi Shuki,

the comparison by match_regex works exactly the way you described it.

Its probably just the pattern, which makes it fail.

Using your example the following iRule returns the expected result:

set RegexStr "\["
set Body [HTTP::payload] ;  >>> [11,222,333,4444]
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]
    append RegexStr "(\\d{$ItemLength}),"
}

 replacing the trailing comma by a closing bracket
regsub {,$} $RegexStr "\]" RegexStr

 regex match
if { [HTTP::payload] matches_regex $RegexStr }  { log "[HTTP::payload] matching regex ($RegexStr)" }

Sending a request by using cURL as follows results in the log message shown below:

curl -v 'http://10.131.131.111/test' --data '[11,222,3333,44444]'
: [11,222,3333,44444] matching regex (\[(\d{2}),(\d{3}),(\d{4}),(\d{5})])

Thanks, Stephan