Selectively read, store text in VB

107 Views Asked by At

I have a long, one line text file in EDI format, and I need to find a way to read and store only a certain part of the file. Throughout the file, there are several instances of "[UNWANTED TEXT]CLM*[NUMBER]*[UNWANTED TEXT]", and I need to be able to grab that [NUMBER] portion, and store it+display it somewhere. I've been able to use Streamreader to read the entire file and display it, but I'm at a loss how to narrow it down to just the parts that I want. Any suggestions?

I'm working in VB.NET now, but I'd be willing to move to another language if it offers an easier way to do this.

Thanks for any help offered.

1

There are 1 best solutions below

2
On BEST ANSWER

You can identify the pattern with Regular Expression and find your desired value through the Regex and Match classes of System.Text.RegularExpressions. Something like this should get you started:

Imports System.Text.RegularExpressions
Public Function getCLM(ediString as string) as string

    Dim regex As New Regex("CLM\*(\d*\.?\d*)")
    Dim match As Match = regex.Match(yourString)
    If match.Success Then Return match.Value
End Function

Dim yourString as String = ""
Dim clmNumber as string = getCLM(yourString)

In this case, you'd have to load your EDI string into yourString. You can see how the regex I've put here functions by using something like Regex101

This is assuming that your string will only have one match of this pattern.