Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit the number of digits in regex

Tags:

python

regex

I have such a regex:

'(?:\$|сум)(\040)?(\d+)|(\d+)(\040)?(?:\$|сум)'

It matches the following strings:

$23
23$
1000сум
сум1000
сум 1000
1000 сум

I want to limit the number of digits in this regex to 8.

Tried this:

'(?:\$|сум)(\040)?(\d{, 8})|(\d{, 8})(\040)?(?:\$|сум)'

It stopped matching anything.

What am I doing wrong?

like image 588
Jahongir Rahmonov Avatar asked Jun 11 '15 09:06

Jahongir Rahmonov


People also ask

How do you restrict length in regex?

By combining the interval quantifier with the surrounding start- and end-of-string anchors, the regex will fail to match if the subject text's length falls outside the desired range.

How do you write the range of a number in regex?

The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. Something like ^[2-9][1-6]$ matches 21 or even 96!

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .


1 Answers

\d{, 8}

Means nothing .Engine will match it literally and so your regex failed.

Use

\d{0,8}

No spaces inside {}

like image 157
vks Avatar answered Oct 05 '22 23:10

vks