# Regexp as string - dynmic

**URL:** <https://community.f5.com/t/regexp-as-string-dynmic/29310>\
**Category:** Technical Forums\
**Tags:** big-iq, devops, application-delivery, ltm\
**Created:** [February 10, 2017, 6:53am UTC](https://community.f5.com/t/regexp-as-string-dynmic/29310 "2017-02-10T06:53:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shuki\_griba\_304](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@Shuki\_griba\_304](https://community.f5.com/u/Shuki_griba_304)\
**Post date:** [February 10, 2017, 6:53am UTC](https://community.f5.com/t/regexp-as-string-dynmic/29310/1 "2017-02-10T06:53:00Z")

</div>

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:

```tcl
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”:

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

```

Thanks

---

<div class="post-metadata">

**Author:** ![Kai\_Wilke](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/kai_wilke/32/24646_2.png) [@Kai\_Wilke](https://community.f5.com/u/Kai_Wilke)\
**Post date:** [February 10, 2017, 8:32am UTC](https://community.f5.com/t/regexp-as-string-dynmic/29310/2 "2017-02-10T08:32:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JRahm](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/jrahm/32/3327_2.png) [@JRahm](https://community.f5.com/u/JRahm)\
**Post date:** [February 10, 2017, 3:16pm UTC](https://community.f5.com/t/regexp-as-string-dynmic/29310/3 "2017-02-10T15:16:53Z")

</div>

Duplicate…[see here](https://devcentral.f5.com/questions/irule-with-dynamic-regexp-51571) for more details.

---

<div class="post-metadata">

**Author:** ![StephanManthey](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@StephanManthey](https://community.f5.com/u/StephanManthey)\
**Post date:** [February 13, 2017, 7:01am UTC](https://community.f5.com/t/regexp-as-string-dynmic/29310/4 "2017-02-13T07:01:06Z")

</div>

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:

```tcl
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:

```tcl
curl -v 'http://10.131.131.111/test' --data '[11,222,3333,44444]'

```

```tcl
: [11,222,3333,44444] matching regex (\[(\d{2}),(\d{3}),(\d{4}),(\d{5})])

```

Thanks, Stephan
