Question

New to Powershell/Regex/EDI. Please no comments on why this shouldn't be done with regular expressions for EDI, I've seen the warnings, but have no choice.

What I need is most likely basic, but I need some help. I have to find all instances of a segment, and retrieve a specific element value from it. The text being searched will be read as one long string, with no CR/LF/etc.

Example data:

~SV1*HC:V2020*35*UN*1***1~DTP*472*D8*20120807~REF*6R*
~SV1*HC:V2100:LT*28.98*UN*1***1~DTP*472*D8*20120807~REF*6R*
~SV1*HC:92014*165*UN*1***1~DTP*472*D8*20120716~REF*6R*

I'm using the following command on another segment and it works just like i want, but it doesn't have to account for non-word characters either:

Select-String -pattern '~svd\*\w+\*(\d+|\d+\.\d+)\*' -input $string -AllMatches | %{$_.Matches} | %{$_.Groups[1]} | %{$_.Value}

Ideally, I would like to find an instance of "~SV1*", skip to the next asterisk, then read everything through the next asterisk. That way it doesn't matter what letter/number/character is in there, it skips it. In the data example above, I would want a return of 35, 28.98, 165. If not, then I can work with what I have, but matching on combinations of word/non-word characters is throwing me, since I don't know what order they may exist in. Everything else I've played with has continued on pulling the rest of the string, and not stopping properly.

If I can get it to do this, I'd be very happy:

~SV1*<skip this>*<get this>*<skip to next SV1>~SV1*<skip this>*<get this>*<skip to next SV1>

Lastly, the data being pulled is a money field, so it may or may not have a decimal present. If there is a cleaner way than (\d+|\d+.\d+), I'm all for it.

Thanks

Was it helpful?

Solution

Use a pattern like this:

 ~sv\d\*[^*]*\*([^*]*)\*

OTHER TIPS

A starting point, but you need to test it:

Select-String -pattern '(?<=~sv\d\*.*\*)(\d*\.?\d+)(?=\*un)' -input $string -AllMatches | %{$_.Matches} | % {$_.Groups[1]} | %{$_.Value}

Using your example data returns 35, 28.98, 165.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top