Question
· Sep 30, 2020

Extracting XML string with separator embedded in text

Trying to extract "x, y", and only the "x" is being extracted because the "," is the separator  

The ":"  is set to extract everything after this 

"Working Example": xyz,

"Not  Working Example": "x,  y",

$EXTRACT($P($P(pData,",",78),":",2),2,$L($P($P(pData,",",78),":",2))-1)

Any ideas on how to extract the whole field on the example with the "," in the field

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

I don't see XML here, but for your examples, I would use Regex

    Set regex = ##class(%Regex.Matcher).%New("(""[^""]*""):\s(""[^""]*""|[^,]*)")     
    
    Set regex.Text = """Working Example"": xyz,"
    Write !,regex.Text,!
    If regex.Locate() {
      Write !?5,regex.Group(1)
      Write !?5,regex.Group(2)
    }
    Write !     
    
    Set regex.Text = """Not  Working Example"": ""x,  y"","
    Write !,regex.Text,!
    If regex.Locate() {
      Write !?5,regex.Group(1)
      Write !?5,regex.Group(2)
    }

And the output will be

"Working Example": xyz, 
     "Working Example"
     xyz

"Not  Working Example": "x,  y",
     "Not  Working Example"
     "x,  y"

Thanks for the suggestions, I wanted to also ask if anyone knows if I could use a line separator (Or a better suggestion) instead of the "," 

I am extracting data from a JSON message into a HL7 and I am trying to identify a unique character I can use as a separator 

For example I could use the "," as the separator but it might be used as free text within the line 

e.g.

"Not  Working Example": "x, y, z, a, b, c",

essentially it could be an infinite amount of characters separated with "," within the message

pure COS solution, split into  steps to allow comments (you may condense it of course)

read  arg    ; get something to work on
"Not  Working Example":"x, y, z, a, b, c",

set name=$piece(arg,":",1)   ; split name from content
set content=$piece(arg,":",2)
if $extract(content,1)=""""  {    ; we got a quuted list
           set value=$piece(content,"""",2)    ;extract first quoted
           set value=$piece(value,",",first,last)  ; pick the relevant pieces
          }
else  {  
       set value=content            ;  no quotes, no pieces
}

 

While you are working with JSON, I would recommend to not do it manually and use native JSON support, available since 2016.2 (2016.1 with notes).

So, you can do it just 

set json = {}.%FromJSON(str)

or

set json = {}.%FromJSON(stream)

and value will be available by

write json."Not  Working Example"

If you have a version with no native JSON support, look at this project