Although Ian did add the caveat “if you are sure of the format” which if interpreted to mean ALWAYS dropping specific indexes after the split() operation it would still “work”. That said, RegEx is the ideal solution here in terms of flexibility and speed. A problem like this just begs for a RegEx solution, IMHO.
As others have said, regular expressions are your friend here.
Var re As New RegEx
Var match As RegExMatch
Var result As String
var linesToSearch() as String = Array("/* RRR */ 1934 /* Yellow */", "/*Brown */ buldog1976 /* row 23 */")
re.SearchPattern = "(?<=\*/\s).+?(?=\s/\*)"
for each lineToSearch as string in linesToSearch()
match = re.Search(lineToSearch)
if match <> nil then
system.DebugLog match.SubExpressionString(0)
end
next
Looks like his intentions are to remove C like remarks. If that was the case, splitting and looking at a fixed place could lead to errors. A regular expression looking for "/* any content including none */" and discarding them seems the way to go.
In order for your solution to work, none of the unintended values that proceed it may contain a “/“ either. Even my regex example begins to fall apart pretty quickly when the inputs deviate from what we assume to expect… and the person that started this thread hasn’t yet been back around to clarify so we are left guessing.
One thing is certain though… I can read your code and intuit its intended function. It’s been only 3 hours since I posted it, and already I cannot glance at my regex sample and intuitively know, specifically, what it is supposed to do.
But hopefully we’ve given the OP some food for thought.
I think @Kem_Tekinay could make a few easy bucks with a “PayPal me $20 and I’ll write a Regex for you” service I love what Regex can do but just looking at it makes my head hurt.
In that case I highly recommend you try out his RegExRX app on his website and try the shareware version or just go purchase it in the mac App Store. On his download page linked above there are numerous other downloads – including some freeware – that are incredibly useful.
Even if you never learn any of the more advanced features of RegEx, what you can do with relatively straight forward pattern matching is INCREDIBLY useful and not hard to “read” once you know a few basics. And performance compared to coding your own searches is astounding.
I’ve always said that RegEx is a write only language. In other words you can create very elegant solutions using it, but you can never understand or maintain them afterwards.
If there is any doubt as to the format RegEx will stand up to it better, that is certain. As I said in my original reply, if you are absolutely certain of the format mine is simple and lightweight.
Hi Chay Wesley, I tried your code and it works … now I just have to figure out how to read from the text file … but I think I can do it.
I try tonight.