2

Hey guys i'm reading lines from .obj files and i want to load them into some arrays but i need to correctly parse them.

I'm reading some lines like this: f 19/19/115 25/25/116 17/22/117

And i want to split them by the "/" but without deleting that character, this is the result i want to obtain:

   [f,19,/,19,/,115,25,/,25,/,116,17,/,22,/,117]

And i'm when i do:

tokens = line.split("[/ ]+");

I'm getting:

[f, 19, 19, 115, 25, 25, 116, 17, 22, 117]
M A
  • 71,713
  • 13
  • 134
  • 174
Miguel Morujão
  • 1,131
  • 1
  • 14
  • 39
  • 3
    https://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html#StringTokenizer%28java.lang.String,%20java.lang.String,%20boolean%29 – JB Nizet Nov 22 '14 at 12:07
  • If you try `string1.replace("/", "#/").split("#");` ? [same example](http://stackoverflow.com/questions/4416425/how-to-split-string-with-some-separator-but-without-removing-that-separator-in-j) – Milaci Nov 22 '14 at 12:08
  • You may find this answer useful: http://stackoverflow.com/questions/2206378/how-to-split-a-string-but-also-keep-the-delimiters. – Iootu Nov 22 '14 at 12:09
  • @Milaci this was my result: [f 1, /1, /1 2, /2, /2 3, /3, /3] – Miguel Morujão Nov 22 '14 at 12:10
  • @Milaci [f,19,/,19,/,115,25,/,25,/,116,17,/,22,/,117] – Miguel Morujão Nov 22 '14 at 12:15
  • This requirement is kind of odd. You want to split by a spacce and by a slash, but only the slash should be in the resulting array? I guess you need call the split* method two times. (* your implementation of it, that can keep the delimiter) – Tom Nov 22 '14 at 12:15
  • From that [link](https://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html#StringTokenizer%28java.lang.String,%20java.lang.String,%20boolean%29) _StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code._ – Reimeus Nov 22 '14 at 12:16
  • already solved, official answer accepted – Miguel Morujão Nov 22 '14 at 12:17
  • @Reimeus but it's not deprecated, works fine, and does the job that split() doesn't do. – JB Nizet Nov 22 '14 at 12:18
  • @manouti How is the "arrays" tag related to this question? The result of `#split` is an array, yes, but this is just an unimportant information. – Tom Feb 01 '15 at 18:04
  • @Tom `split` returns an "array" of strings... And the OP wants to store results in an "array". – M A Feb 01 '15 at 18:07
  • @manouti The array is not the question here. The question is "how to keep the delimiter". During a review you can reject edits like this perfectly with this text: `Tags should help to describe what the question is about, not just what it contains.`. The array not is just uninteresting "side effect". – Tom Feb 01 '15 at 18:17

2 Answers2

1

Try the following:

tokens = line.split("(?<=/+)|(?=/+)| +");

(?<=/+): match one or more / in lookbehind expression

(?=/+): match one or more / in lookahead expression

M A
  • 71,713
  • 13
  • 134
  • 174
1

Try this:

System.out.println(Arrays.toString("f 19/19/115 25/25/116 17/22/117".replace("/","#/#").split("[# ]")));
Sarthak Mittal
  • 5,794
  • 2
  • 24
  • 44