1

I'm beginning to write my own dxf file parser and I've ran across a regex problem. Consider the following text file (which is a snippet of a particular dxf file I'm working on):

http://www.filedropper.com/test_104

I read this file in as a String with:

String s = FileUtils.readFileToString(file);

I then want to use regex to split this string so I get an array of Strings of size two with the LINE entity as the first element and the MTEXT entity as the second. My first thought was to use:

String[] tokens = s.split("\\s{2,2}0");

The problem with this however (which you can test for yourself), is that it returns:

{"", "\nLINE...", "\nMTEXT...", "\n100...", "\n"}

Of course the first and last strings could easily be removed from the array, but if you look at the text file, you'll see that in the MTEXT entity there is

"    0"

i.e, four whitespaces and a 0. I don't want to split on this unfortunately. So my question is: how can I parse this using split and regex to simply obtain the array:

String[] tokens = {"\nLINE...", "\nMTEXT..."}

1 Answer 1

1

You can use positive look-ahead.

s.split("\\s\\s0(?=\\nLINE|\\nMTEXT)");

Sign up to request clarification or add additional context in comments.

3 Comments

Your absolutely correct. I had just noticed to that you could do: s.split("\\n\\s\\s0\\n") because of the way the end line characters are setup in the file.
Another question. Is there a way to compactify your above regex expression when testing all dxf entity objects. This list can be found here: link
Well if you found a good delimited like "\\n\\s\\s0\\n" then I recommend you use that. Generally I don't use split since sometimes it can be nearly impossible to construct an expression that precisely splits. I tend to use Pattern and Matcher object directly and examine matches for additional conditions (such as match starting one of particular keywords). I manually glue together matches that don't statify the condition to get the correct List of string fragments.

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.