开发者

Matching multiple input values in regex

I have the following input values and I was hoping someone could kindly 开发者_C百科tell me the best way to match all of the "values" (217, 218, 219) -- The html is escaped and that's what I need to match.

<input type=\"hidden\" name=\"id\" value=\"217\"\/>

<input type=\"hidden\" name=\"id\" value=\"218\"\/>

<input type=\"hidden\" name=\"id\" value=\"219\"\/>


Based on your comments to other responses, you actually only want to match on the numbers if they are fit the pattern name=\"id\" value=\"###\" and thus there are four possibilities, depending on how precise you want your matches to be. Also, based on your comments, I'm using javascript as the language of implementation.

Also, note that a previous answer incorrectly escaped the slashes around the id and value strings.

FWIW, I have tested each of the options below:

Option 1: Match any number

//build the pattern
var pattern = /name=\"id\" value=\"([0-9]+)\"/g

//run the regex, after which:
//  the full match will be in array_matches[0]
//  the matching number will be in array_matches[1]
var array_matches = pattern.exec(strVal);

Option 2: Match any 3-digit number

//build the pattern
var pattern = /name=\"id\" value=\"([0-9]{3})\"/g

//run the regex, after which:
//  the full match will be in array_matches[0]
//  the matching number will be in array_matches[1]
var array_matches = pattern.exec(strVal);

Option 3: Match specific 3-digit number ranges

//build the pattern; modify to fit your ranges
//  This example matches 110-159 and 210-259
var pattern = /name=\"id\" value=\"([1-2][1-5][0-9])\"/g

//run the regex, after which:
//  the full match will be in array_matches[0]
//  the matching number will be in array_matches[1]
var array_matches = pattern.exec(strVal);

Option 4: Match specific 3-digit numbers

//build the pattern; modify to fit your numbers
//  This example matches 217, 218, 219 and 253
var pattern = /name=\"id\" value=\"(217|218|219|253)\"/g

//run the regex, after which:
//  the full match will be in array_matches[0]
//  the matching number will be in array_matches[1]
var array_matches = pattern.exec(strVal);


I don'ty know in what language, but supposing the code you showed is a string (since you escaped all quotes) you can pass that string into the following regexp that is going to match any sequence of numbers.

/([0-9]+)/g

Based on the language you use you need to port this regexp and use the proper function.

In JS u can use:

var array_matches = "your string".match(/([0-9]+)/g);

In PHP u can use:

preg_match("([0-9]+)", "your string", array_matches);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜