-1

I have a example String: abcd $cvzasd$ waedqwe$qewrerw$dsaxcz $dasdasd$ $fowjew$ dsadsa. The $ sign is my delimiter. And I want split this String, but not removing delimiter $. I want at result list with following 8 elements:

abcd, $cvzasd$, waedqwe, $qewrerw$, waedqwe, dsaxcz, $dasdasd$, $fowjew$, dsadsa 

How I can't get that result? It's possible, create regex for that? I found a good example: Java: Split String with Regex without deleting delimiters but this not satisfy me because in example use delimiters < and > and it works only for different delimiter.

Otherwise I found a solution with replace my sign by adding to my sign additional sign. Example replace $ by -$ and split by -. But I think is not good solution for me.

Community
  • 1
  • 1
The Nightmare
  • 701
  • 5
  • 16
  • 36
  • 4
    that looks more like a whitespace is the delimiter. Why would cvzasd have a $ on both sides? – luk2302 Jun 29 '16 at 17:08
  • I'm also confused by how you are splitting by '$'. In some cases you are splitting before and other cases afterward. You also have two '$' in a row, which would produce an empty element. – micker Jun 29 '16 at 17:09
  • 1
    It really looks like you need to split with `\\s+`, see http://ideone.com/qltEjq. Are you sure of the expected output? – Wiktor Stribiżew Jun 29 '16 at 17:09
  • Sorry. I I chose the wrong example. The $ must be my delimiter, because then I replace the word between $ and I must know, which word is between $ signs. I edited first post. – The Nightmare Jun 29 '16 at 17:17
  • How do you determine which words are between dollar signs and which ones are not? `waedqwe$qewrerw$dsaxcz$waedqwe$qewrerw$dsaxcz$` Who should get them in a string like this? – TemporalWolf Jun 29 '16 at 17:24
  • 1
    Your example is still very bad because the first match should either be `abcd ` or `abcd $` but not just `abcd`. You should firstly get VERY clear on what the exact outcome should be and why - Java will not do what you *think*, it will do what you tell it to do. Right know even the other humans here do not understand what you want - Java will have even more trouble ;) – luk2302 Jun 29 '16 at 17:44

2 Answers2

1

You can leverage the constrained-width lookbehind supported by Java regex engine (the {0,100} limited quantifier is allowed in Java lookbehinds, just make sure you adjust the value in case you have longer values):

\s+|(?=(?<!\$\w{0,100})(?<!\s)\$)|(?<=\$(?!\w{0,100}\$))

See the regex demo

Java demo:

String pat = "\\s+|(?=(?<!\\$\\w{0,100})(?<!\\s)\\$)|(?<=\\$(?!\\w{0,100}\\$))";
String s = "abcd $cvzasd$ waedqwe$qewrerw$dsaxcz $dasdasd$ $fowjew$ dsadsa";
String[] res = s.split(pat);
System.out.println(Arrays.toString(res));
// => [abcd, $cvzasd$, waedqwe, $qewrerw$, dsaxcz, $dasdasd$, $fowjew$, dsadsa]
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
-1

Like luk2302 says, you can use a whitespace as a delimiter which separates an item in the list from another:

public class SplitEx {
    public static void main(String[] args) {
        String s = "abcd $cvzasd$ waedqwe $dasdasd$ $fowjew$ dsadsa";
        String[] parts = s.split("\\s+"); // note: returns an array
        System.out.println(Arrays.toString(parts));
    }
}

prints:

[abcd, $cvzasd$, waedqwe, $dasdasd$, $fowjew$, dsadsa]
Kedar Mhaswade
  • 4,535
  • 2
  • 25
  • 34
  • this will not work for the OP's original string containing "waedqwe$qewrerw$dsaxcz" Note that unlike your example there are no spaces here, but the OP wants this to resolve to "waedqwe, $qewrerw$, dsaxcz" – FredK Jun 29 '16 at 17:24
  • It is still not clear if the original string does _not_ have spaces and the confusion is not only mine. – Kedar Mhaswade Jun 29 '16 at 18:26