In a recent use of String.split()
, I was faced with a situation where the text was so dynamic, it is easier to pick up the matches than to filter out the non-matches.
I caught myself wondering if it's possible to modify a "reverse regex" for String.split()
so that you can give it any pattern and it will match every group of characters that does NOT match that pattern.
*NOTE: The "problem" here can easily solved with String.matches()
, Tokens
, Matcher.group()
, etc. This question is mostly hypothetical (code samples are still welcome, as the question's nature pretty much requires it), and it's not about how to achieve the results, but about if it's a possible to achieve them this way.
What i tried:
String pattern1 = "(test)"; //A verif. that what "should-not-match" is working correctly.
String pattern2 = "[^(test)]"; //FAIL - unmatches the letters separately.
String pattern3 = "(^(test))"; //FAIL - does not match anything, it seems.
String text = ""
+ "This is a test. "
+ "This test should (?not?) match the word \"test\", whenever it appears.\n"
+ "This is about to test if a \"String.split()\" can be used in a different way.\n"
+ "By the way, \"testing\" does not equal \"test\","
+ "but it will split in the middle because it contains \"test\".";
for (String s : text.split(pattern3)) {
System.out.println(s);
}
And other, similar patterns, none of which was anywhere near successful.
UPDATE:
I have now attempted a few patterns using the special constructors as well, but didn't get it to work yet either.
As for what i want, following the "test" example, is to get an array containing strings whose content is "text" (What i want to use as base pattern, or in other words what i want to FIND).
But do this using String.split()
, with makes using the base pattern directly result in "whatever is not (test)", thus needing a reversal in order to result "just the occurrences of (test)".
Bible-sized-long-story-short, the wanted is regex for String.split()
that results in this behavior (+result):
NOTE: follows the example code above, including needed variables (text).
String[] trash = text.split("test"); //<-base pattern, needs reversing.
System.out.println("\n\nWhat should match the split-pattern (due reversal), become separators, and be filtered out:");
for (String s : trash) {
System.out.println("[" + s + "]");
text = text.replace(s, "%!%"); //<-simulated wanted behavior.
}
System.out.println("\n\nWhat should be the resulting String[]:");
for (String s : text.split("%!%")) {
System.out.println(s);
}
System.out.println("Note: There is a blank @ index [0], since if the text does not start with \"test\", there is a sep. between. This is NOT WRONG.");
Code samples are welcome. The possibility (or not) to create such code is this question's nature after all.