Written by

Question Lionel Woods · 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

Comments

Robert Cemper · Sep 30, 2020

using 

$P(pData,",",78,79)

should do it 

0
Lionel Woods  Sep 30, 2020 to Robert Cemper

This worked, thank you!

0
Dmitry Maslennikov · Sep 30, 2020

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"
0
Ba Moser · Sep 30, 2020

looks more like JSON than XML

0
Lionel Woods · Oct 1, 2020

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

0
Robert Cemper  Oct 1, 2020 to Lionel Woods

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
}

 
0
Dmitry Maslennikov  Oct 2, 2020 to Lionel Woods

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

0