Skip to main content
joes0005
February 22, 2022
Solved

How to parse data from a string, specifically how do I find the index of the first character that's a number?

  • February 22, 2022
  • 10 replies
  • 0 views

So I've got an API integration that returns data in a single string field and I'd like to extract sections of the data to put into separate CDT fields. So the string that's returned will look something like this: "Advil 20 MG/ML Oral Suspension". I'd like to retrieve the data before the number, then everything after including the number. I've posted what I have so far below. What I'm wondering is how do I get the index of the first character that's a number? If I get that, then I can figure out the rest. Thanks!

a!forEach(
  items: rule!NLH_GetPrescriptionDrugs(searchTerm: ri!search).result.body.drugGroup.conceptGroup.conceptProperties,
  expression: a!forEach(
    items: fv!item,
    expression: 'type!{urn:com:appian:types:NLH}NLH_Prescription'(
      rxcui: fv!item.rxcui,
      medication: fv!item.synonym
    )
  )
)

Best answer by gopalk2865

Yes, In that case null check can be applied,

a!localVariables(
  local!string: "Advil xx MG/ML Oral Suspension",
  if(
    a!isNullOrEmpty(regexfirstmatch("[0-9]", local!string)),
    "",
    find(
      regexfirstmatch("[0-9]", local!string),
      local!string
    )
  )
)

10 replies

stewart.burchell
February 22, 2022

I'm sure there are more elegant ways, but you can use the following fragment to return a list of indices for every possible numeric value:

fn!wherecontains(
  {"0","1","2","3","4","5","6","7","8","9"},
  fn!char(fn!code("Advil 20 MG/ML Oral Suspension"))
)

If the returned array is empty, then your string contains no numerics, otherwise the first item in the array is the index of the first number.

gopalk2865
Participating Frequently
February 22, 2022

Hi, You can try below code as well.

a!localVariables(
  local!string:"Advil 20 MG/ML Oral Suspension",
  find(regexfirstmatch("[0-9]",local!string),local!string)
  
)

stewart.burchell
February 22, 2022

Neater...but if the string contains no numbers it throws an error:

a!localVariables(
  local!string:"Advil XX MG/ML Oral Suspension",
  find(regexfirstmatch("[0-9]",local!string),local!string)

)

...results in:

gopalk2865
Participating Frequently
February 22, 2022

Yes, In that case null check can be applied,

a!localVariables(
  local!string: "Advil xx MG/ML Oral Suspension",
  if(
    a!isNullOrEmpty(regexfirstmatch("[0-9]", local!string)),
    "",
    find(
      regexfirstmatch("[0-9]", local!string),
      local!string
    )
  )
)

richardm900458
February 22, 2022

try this for string
 

a!localVariables(
  local!string:"Advil XX MG/ML Oral Suspension",
  stripwith(tostring(regexallmatches("[0-9]",local!string)), "; ")
)

This one if you want to archive an integer output:
a!localVariables(
  local!string:"Advil XX MG/ML 20 Oral Suspension",
  tointeger(tostring(regexallmatches("[0-9]",local!string)))
)