How do I write a Regular Expression to match any three digit number value?

import re

data = "719"
data1 = "79"

# This expression will match any single, double or triple digit Number
expression = '[\d]{1,3}'

print(re.search(expression, data).string)

# This expression will match only triple digit Number
expression1 = '[\d]{3}'

print(re.search(expression1, data1).string)

Output :

expression : 719

expression1 : 79


/sdval="\d{3}"/

EDIT:

To answer your comment, \d in regular expressions means match any digit, and the {n} construct means repeat the previous item n times.


This should do (ignores leading zeros):

[1-9][0-9]{0,2}

Easiest, most portable: [0-9][0-9][0-9]

More "modern": \d{3}

Tags:

Regex