1

I would like to parse just one long line from file based on " and , and [ and ] and { and }

For e.g

{"ColumnTypes":"int32","fstring32","int32"],"ColumnNames":"ProductId",","ProductName","Quantity"]}

I actually need two different array from this line column types and column names. I tried string.split("\\W") but it is not working.

Please guide. Thanks in advance.

4
  • And you're 100% certain that these characters will not be a part of your string literals? Commented Jun 26, 2013 at 12:32
  • 2
    All those braces and brackets are heavily unbalanced. Are they really like that? If they aren't, this almost looks like JSON, so you could parse your string much more sensibly. Commented Jun 26, 2013 at 12:33
  • Hi @TimPietzcker thanks a lot for the input. Yes it may be JSON I am not sure can you please guide me how can I parse it. It is written inside plain schema text file. Commented Jun 26, 2013 at 12:58
  • 1
    Try json.org/java, that should contain all the information you need. Commented Jun 26, 2013 at 14:48

2 Answers 2

2

You need to split on multiple \\W chars (add a +), but first trim off such chars from the front, so you don't get a blank first element:

String[] array = string.replaceAll("^\\W+", "").split("\\W+");
Sign up to request clarification or add additional context in comments.

1 Comment

@TimPietzcker No need. split() drops all trailing blanks from the result. To include the blanks you must call split() with a second parameter of -1 (or a very large number).
1

The simplest way (with regex) would be to allow more than one non-word character to match:

string.split("\\W+")

3 Comments

Try using regex notation /\W+/ instead of "\\W+"
Whoops. No you are right. I had a brain fart. Was thinking JavaScript
Is groovy notation also :)

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.