Reputation: 776
I am writing a VB.net program to detect a words. Here is my sample text file:
EXTERNAL 16EVP_PK -- FL(4) TDC PERMIT CONTRL TO MESTO CONTRL
-- FL(5) $HY04B09.BOXTSTAT (= RUN = ON)
EXTERNAL 16EVPUPK -- FL(33) REQUEST FROM METSO FOR CONTRL
EXTERNAL 16SA0541 -- SP VALUES TRANSFERS
EXTERNAL 16FC0730, 16FC0815, 16FC0830 -- Hiway 4 Tags
EXTERNAL 16FC1525 -- Hiway 4 Tags
I want to detect everything after EXTERNAL and before -- (EXTERNAL can be external or ExTernal or any combination) . The first match should read "16EVP_PK" second match 16EVPUPK third match 16SA0541 fourth match "16FC0730, 16FC0815, 16FC0830" and so on
Here is the code that I have written:
Private Sub CheckCLFile()
path="D:\16METEVP.CL"
'Read CL file
Dim value As String = System.IO.File.ReadAllText(path)
Dim Pattern As String = "(?m)(?<=\bExternal).*$"
Debug.WriteLine(value)
Debug.WriteLine(Regex.Matches(value, Pattern).Count())
For Each m As Match In Regex.Matches(value, Pattern)
Console.WriteLine("'{0}' found at index {1}.",
m.Value, m.Index)
Next
End Sub
Thanks in advance.
Upvotes: 0
Views: 1745
Reputation: 10360
Try this Regex:
(?<=EXTERNAL)\s*.*?(?=\s*--)
With this you will get extra leading spaces before each match. I guess, you can easily trim that before using the matches.
Explanation:
(?<=EXTERNAL)
- Positive lookbehind to find the position immediately preceded by EXTERNAL
. Switch the Ignore Case
flag ON.\s*
- matches 0+ occurrences of white-spaces, as many as possible.*?
- This is your actual match. It matches 0+ occurrences of any character but a new-line character, as few as possible(?=\s*--)
- Positive lookahead to validate that the match must be followed by 0+ spaces followed by --
Upvotes: 1