views:

61

answers:

3

Hi

I have the following input values and I was hoping someone could kindly 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\"\/>
A: 

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);
Marco Demajo
I should have mentioned: there is more to the document that should have numbers... I need to narrow it down to name=\"id\" value=
UltraVi01
@UltraVi01: Then JGB146 answered you properly, read my comments to his answer there.
Marco Demajo
A: 

try this regex :

~value=\\"(\d+)\\"\\/>~g
M42
Could the downvoter explain the reason why ? This regex match the string `value=\"217\"\/>` and the other ones given in example.
M42
I wasn't the one who downvoted, but your extra \ chars (which, I assume, are intended to escape the other \ chars) are not needed and will actually cause the match to fail, at least in javascript (which is the language the author is using).
JGB146
@JGB146: Thanks for explanation, but according to `http://www.regular-expressions.info/javascriptexample.html` it is mandatory to escape the backslashs.
M42
@M42: Interesting. I certainly would have expected them to be required. Most of my regex exp is in php, where they definitely are. It's very strange that having them here leads the match to fail, and that it succeeds otherwise.
JGB146
I guess the other possible reason for their downvote is that ~ is not a valid delimiter in Javascript.
JGB146
@JGB146: May be, who knows ? Nevertheless thanks again for your comments.
M42
+1  A: 

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);
JGB146
@UltraVi01: JGB146 answered you properly unless now you tell us that in the same string you might have other HTML tags containing attributes in the format name="id" value="nnn" that are not input tags, like textarea or checkbox or text. In that case you need to make the regex even more specific, like: var pattern = /<input type=\"hidden\" name=\"id\" value=\"([0-9]+)\"/g
Marco Demajo