开发者

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

I'm working with some pretty funky HTML markup that I inherited, and I need to remove the following attributes from about 72 td elements.

sdval="285"

I know I can do this with find/replace in my code editor, except since the value of each attribute is different by 5 degree increments, I can't match them all without a Regular Expression. (FYI I'm using Esspress and it does support RegExes in it's Find/Replace tool)

Only trouble is, I really can't figure out how to write a RegEx for this value. I understand the concept of RegExes, but really don't know how to use them.

So how would I write the following with a Regular Expression in place of the dig开发者_如何学编程its so that it would match any three digit value?

sdval="285"


/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.


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

More "modern": \d{3}


This should do (ignores leading zeros):

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


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


It sounds like you're trying to do a find / replace in Visual Studio of a 3 digit number (references to Express and Find/Replace tool). If that's the case the regex to find a 3 digit number in Visual Studio is the following

<:d:d:d>

Breakdown

  • The < and > establish a word boundary to make sure we don't match a number subset.
  • Each :d entry matches a single digit.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜