Article Padmaja Konduru · Jan 14 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

Comments

Yaron Munz · Jan 14

to be able to handle endStr in any length you should have:
S value = $E(text, start, end-$L(endStr)-1)

0
Padmaja Konduru  Jan 14 to Yaron Munz

Thank you Yaron for suggestion, it has been updated. Both the lines provide the same result.

Thanks,

0
David Underhill · Sep 26

Just as an example, an alternative with $piece.
An issue would be if both startStr and stopStr are the same value which $find does solve.

set values=""for n=2:1:$length(text,startStr) {
    set check=$piece(text,startStr,n)
    if check[stopStr set values=values_" "_$piece(check,stopStr)
return$piece(values," ",2,*)
0