1

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\"\/>
3
  • Or which IDE? [stupid comment min length limit!] Sep 3, 2010 at 17:24
  • Are all the lines in a single string ? Or are they read from a file ?
    – Toto
    Sep 3, 2010 at 17:34
  • Javascript, it's for a jMeter test.. Read from a JSON response.. so single line
    – RyanLynch
    Sep 3, 2010 at 17:37

2 Answers 2

4

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);
1
  • @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 Sep 6, 2010 at 11:52
1

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);
2
  • I should have mentioned: there is more to the document that should have numbers... I need to narrow it down to name=\"id\" value=
    – RyanLynch
    Sep 3, 2010 at 17:41
  • @UltraVi01: Then JGB146 answered you properly, read my comments to his answer there. Sep 6, 2010 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.