SangminKim
SangminKim

Reputation: 9136

Nginx regular expressions `{number}` doesn't work appropriately

I want to use if statement regular expression on a query string So I code like below.

if ($arg_arg1 ~* [A-Z0-9]{3}_[ -~]{1,28}) {
                                echo "print something;
                        }

But when I reload the configuration, it returns nginx: [emerg] invalid condition "[A-Z0-9]" in /etc/nginx/nginx.conf:29.

Later I fix [A-Z0-9]{3}_[ -~]{1,28} to "[A-Z0-9]{3}_[ -~]{1,28}" adding ".

Why do I have to enclose " ?

Even though I can reload the configuration with "

It still has a problem.

It works not as I expect.

It Must follow below rules.

  1. exact 3 char ranging A-Z or 0-9
  2. 1 char _
  3. 1~28 char ranging " "-~

for example AAA_11, ABC_123123123, EEE_asdasdasd, 123_123123.

But It passes below wrong conditions

aaa_123 (the first three char Must be upper capital)

aaaa_123 (aaaa is more than 3 char)

I tested my regular express on Python code .

import re 
r = re.match("[A-Z0-9]{3}_[ -~]{1,28}", "123_aaa")
print r.string

Upvotes: 0

Views: 2070

Answers (1)

SangminKim
SangminKim

Reputation: 9136

Sorry, I did stupid things.

I just add ^ and &.

I means from "[A-Z0-9]{3}_[ -~]{1,28}", "123_aaa" to "^[A-Z0-9]{3}_[ -~]{1,28}$", "123_aaa"and from ~* to ~.

Test your regex here http://www.regexpal.com/ before trying if you are a novice at regex.

It works perfectly!

Upvotes: 1

Related Questions