Reputation: 6202
I want to split a line of coordinates and signs like: (4, 9) ?, (741, 5) +, (555, 82) -,
To be split to the following parts: 4, 9, ?, 741, 5, +, 555, 82, -
So I came up with this expression: ",(?=([0-9]+))|(?<=([0-9]+)),| +|\\(|\\)?"
but it matches digits instead of numbers and a lot of empty strings... How can I fix it?
The expression is supposed to match a number if there's a ,
behind or infront of it, and the other +-?
signs.
This is using String.split()
.
Upvotes: 1
Views: 98
Reputation: 60143
Instead of splitting, how about just searching for what you're looking for?
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String... args) {
String input = "(4, 9) ?, (741, 5) +, (555, 82) -,";
Matcher matcher = Pattern.compile("[\\d?+\\-]+").matcher(input);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
// Output:
// 4
// 9
// ?
// 741
// 5
// +
// 555
// 82
// -
Depending on your input, this regular expression may be even better:
\\d+|[?+\\-]
For the given input, the result is the same, but this version would mean sequences like "45-8" result in three matches (45
, -
, and 8
) instead of just one (45-8
).
Upvotes: 2
Reputation: 626950
Here is a split solution:
String s = "(4, 9) ?, (741, 5) +, (555, 82) -,";
String pat = "[\\D&&[^+?-]]+";
String[] res = s.replaceFirst("^"+pat, "").split(pat);
System.out.println(Arrays.toString(res));
See the regex demo
The [\D&&[^+?-]]+
pattern matches one or more non-digit symbols except +
, ?
, -
. The replaceFirst
with a ^
anchor prepended to the pattern removes the matches from the start of the string to get rid of an empty array element in the result. The &&[^...]
here is a character class subtraction syntax, we match all what is before except what is after ^
. Note that \D
(any non-digit) is equal to [^\d]
(any character other than a digit) thus, you can actually write the same pattern as [^\d+?-]
.
Since you asked:
Can we apply de morgan on this and use the expression "a number or +-?"?
It depends on what you mean by a number, but it is possible you want to match a chunk of digits, OR a +
, -
or ?
. In this case, you really need an alternation: \d+|[-?+]
.
String rx = "\\d+|[-?+]";
List<String> lst = new ArrayList<>();
Pattern p = Pattern.compile(rx);
Matcher m = p.matcher("(4, 9) ?, (741, 5) +, (555, 82) -,");
while (m.find()) {
lst.add(m.group(0));
}
System.out.println(lst);
See Java demo
Upvotes: 1