B4J Question How To Regex This Text

cklester

Well-Known Member
Licensed User
Can someone show me the regex to split each line of the following text into a three-member string array split by the '=' and ':'?

***Available04***=2432323626:C214644
***Available48***=3602856203:C233263
***Available03***=746737721:C232625
James Bond=2558963298:C215712
***Available34***=2920095376:C228842
Kevin Costner=1855544359:C225971
Milla Jovovich=788300210:C225043
Paul Simon=2758362877:C216819
***Available07***=3959694571:C218632
Old McDonald=1888642090:C215545
***Available11***=1143731582:C216879
Marcus Aurelius=1523664491:C223065
 

cklester

Well-Known Member
Licensed User
ANSWERED! Found here.

Using Regex.Split("=|:",my_text) for each line does the work.

Let me know if there's a better way! (I've seen a reference to MULTI_LINE, so maybe I don't have to read each line of the file...)
 
Upvote 0

drgottjr

Well-Known Member
Licensed User
Longtime User
don't be "ascairt", try it. you'll see how it treats your multiline string.

use
B4X:
 dim text as string = file.readstring( your_file )
to read in your file,

then apply your constraint
B4X:
 Dim splitter As List = Regex.Split2( "=:", Regex.MULTILINE, text)
,

then step through your splitter to see what you got.

it might look the same as before at first glance, but it's not. you'll have to look closely to see why. the MULTILINE constraint essentially treats your list of "records" as a 1-line string (which it isn't). that's the purpose of the constraint. when you use MULTILINE, you're basically saying, "i know we're looking at a bunch of discrete strings (separated by CRLF), but i want you to ignore that, and make like it's just one big string." this screws with the regex splitter because it makes the beginning of the "next" string appear like part of the "last" string. the CRLF is needed to divide each "record" into 3 elements.

you would use MULTILINE, for example, if you're scraping a web page looking for something. let's say you're looking for 2 particular words in succession. well, if word 1 appears on one line and word 2 appears on the next line, regex won't find the match. you need to search for the words using the MULTILINE constraint.
 
Upvote 0
Top