Article
· 16 hr ago 2m read

Find Values from Text and Display

The utility returns the desired values from the text and display the multiple values if exists based on starting and ending string.

Class Test.Utility.FunctionSet Extends %RegisteredObject
{

/// W !,##class(Test.Utility.FunctionSet).ExtractValues("Some random text VALUE=12345; some other VALUE=2345; more text VALUE=345678;","VALUE=",";")
 

ClassMethod ExtractValues(text As %String, startStr As %String, endStr As %String) As %String
{    //Initialize Valriables
   Set values = ""
   Set start = 1
   
   While start '= 0 {
 Set start = $FIND(text, startStr, start)
 IF start = 0 QUIT }
     Set end = $FIND(text, endStr, start)
     IF end = 0 QUIT }
    //S value = $E(text, start, end-2)
     value = $E(text, start, end-$L(endStr)-1)
     IF values '= "" {
  Set values = values _" "_value   
     }Else {
  values = value   
     }
     start = end
   }
    values
} }

Output:

W !,##class(Test.Utility.FunctionSet).ExtractValues("Some random text VALUE=12345; some other VALUE=2345; more text VALUE=345678;","VALUE=",";")

12345 2345 345678

Discussion (2)1
Log in or sign up to continue