0

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.

1 Answer 1

1

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.

Click for Demo

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 --
Sign up to request clarification or add additional context in comments.

5 Comments

Gurman - very close, but where 3 tags (16FC0730, 16FC0815, 16FC0830 ) needs to be selected, I see only one (16FC0730). I need to select everything after External but before --. Thanks.
Hi...I hadn't read that properly..I have now updated the solution
Very nice explanation. Learned something new about Regex. Thanks Gurman.
Using .NET, thus no need for the lookbehind to be fixed length, no need to trim leading spaces if \s* goes into it.
@PJProudhon Thanks for that information. I didn't know about that. Here is the updated solution (?<=EXTERNAL\s+).*?(?=\s*--)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.