0

Fairly new to classic ASP(maintaining legacy applications) and I need to figure out how to fish out values from a string. String itself can look something like this - 0,12,234,543. I was thinking about making a function where I can specify which number I want from the string for ex.

Function fnGetNumber(string, 3)
  // returns the third number(number after the second comma ie. 234)
End Function

The string will always have only numbers and always 4 of them. Also they will not have decimal places.

The function itself is not a problem, but I can't figure out the regex.

  • 1
    No need for RegEx, use [Split](http://www.devguru.com/technologies/vbscript/13973). – Passerby Jul 12 '13 at 08:35
  • @Passerby Due to the strings coming from the database and they're being displayed through iteration I can not use `Split(records("string"), ",")`, will return an error. – user2575487 Jul 12 '13 at 08:41
  • What error? `Split` _returns_ an array, not _convert_ the string into an array. As long as you provide a valid string, it will work. – Passerby Jul 12 '13 at 08:50
  • I know. Error - "Cannot use parentheses when calling a Sub". – user2575487 Jul 12 '13 at 08:55
  • 2
    That's just a syntax error, and it's about a line calling a Sub, which doesn't seem to have something to do with your posted question. Can you post more related code? – Passerby Jul 12 '13 at 09:15
  • My question seems similar and has regexp answer in it; http://stackoverflow.com/questions/17609031/handling-series-of-comma-separated-values-in-vba – Raybarg Jul 12 '13 at 09:22
  • the function you showed is what, always returning the third number? You can use Instr and find the 2nd comma, then again on the 3rd comma, then use mid and go from the first instr to the second instr value. You should use split though, and take the value from the array. – prospector Jul 14 '13 at 07:43

1 Answers1

0

Using Split is the way, use it to transform the string to an array and pull the data from that.

I wouldn't recommend regex as whilst it powerful, its readability is poor.

back_ache
  • 382
  • 1
  • 13