I have strings of the form:
"abc" 1 2 1 13
"efgh" 2 5
Basically, a string in quotes followed by numbers separated by whitespace characters.
I need to extract the string and the numbers out of the line.
So for eg., for the first line, I’d want
abcto be stored in aStringvariable (i.e. without the quotations) and- an array of
intto store[1,2,1,13].
I tried to create a pattern that’d do this, but I’m a little confused.
Pattern P = Pattern.compile("\A\".+\"(\s\d+)+");
Not sure how to proceed now. I realized that with this pattern I’d kinda be extracting the whole line out? Perhaps multiple patterns would help?
Pattern P1 = Pattern.compile("\A\".+\"");
Pattern P2 = Pattern.compile("(\s\d+)+");
Again, not very sure how to get the string and ints out of the line though. Any help is appreciated!
I would rather just split the string on space, rather than building complex regex, and use it with
PatternandMatcherclass.Something like this: –
OUTPUT: –
Shows your intent much clearer, that what you want to do.
Then, you can get the
stringandintegerparts from your string array. You would need to do aInteger.parseInt()on integer elements.If your string may contain spaces in it, then in that case, you would need a
Regex. Better one would be the one in@m.buettner'sanswer