# Variable interpolation in a regex

**URL:** <https://www.ruby-forum.com/t/variable-interpolation-in-a-regex/222654>\
**Category:** NGINX\
**Created:** [September 4, 2012, 9:55pm UTC](https://www.ruby-forum.com/t/variable-interpolation-in-a-regex/222654 "2012-09-04T21:55:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [September 4, 2012, 9:55pm UTC](https://www.ruby-forum.com/t/variable-interpolation-in-a-regex/222654/1 "2012-09-04T21:55:32Z")

</div>

Hello All,

I have a map block:

```
    map $http_cookie $myVal {
            "~adq_cnv(\d+)=($cmpid[^;]+#(?P<DC>\w{2}))(?:;|$)" $DC;
            default "XYZ";
    }

```

The $cmpid used in the left side of a map is to be interpolated from a  
location block :

```
   location ~ ^/cnv/(\d+)/ {
             set $cmpid $1;
              ...
   }

```

But that is not working. I tried different variations like : ${cmpid},  
$cmpid . But to no luck.

Any help would be appreciated.

Thanks  
Harish

---

<div class="post-metadata">

**Author:** ![harish\_sri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a88e57/32.png) [@harish\_sri](https://www.ruby-forum.com/u/harish_sri)\
**Post date:** [September 4, 2012, 10:03pm UTC](https://www.ruby-forum.com/t/variable-interpolation-in-a-regex/222654/2 "2012-09-04T22:03:42Z")

</div>

On Tuesday 04 September 2012 23:55:07 Harish S. wrote:

> Hello All,
> 
> I have a map block:
> 
> ```
> map $http_cookie $myVal {
> "~adq_cnv(\d+)=($cmpid[^;]+#(?P<DC>\w{2}))(?:;|$)" $DC;
> default "XYZ";
> }
> 
> ```

[…]

> But that is not working. I tried different variations like : ${cmpid},  
> $cmpid . But to no luck.
> 
> Any help would be appreciated.

Regular expressions in config file are compiled at the configuration  
phase, so  
you can’t use variables there.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![harish\_sri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a88e57/32.png) [@harish\_sri](https://www.ruby-forum.com/u/harish_sri)\
**Post date:** [September 4, 2012, 10:47pm UTC](https://www.ruby-forum.com/t/variable-interpolation-in-a-regex/222654/3 "2012-09-04T22:47:01Z")

</div>

Hi Valentin,  
Thanks. Is there any way to achieve the same thing through some other  
means, even if it takes a hit on the performance ?

Thanks  
Harish
