Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

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 digits so that it would match any three digit value?

sdval="285" 
like image 495
Joel Glovier Avatar asked Nov 24 '10 21:11

Joel Glovier


People also ask

How do you match a regular expression with digits?

To match any number from 0 to 9 we use \d in regex. It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range.

What can be matched using (*) in a regular expression?

A regular expression followed by an asterisk ( * ) matches zero or more occurrences of the regular expression. If there is any choice, the first matching string in a line is used.


1 Answers

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

like image 164
Alex Avatar answered Sep 20 '22 08:09

Alex