J
J
Jake Taylor2021-05-03 16:14:54
Java
Jake Taylor, 2021-05-03 16:14:54

Regular expression question with "any character except" condition in Java?

Why is the regular expression not working as intended?
Source string:
String source = "-1 - -2233 -3- -4";

Regular expression:
String NUMBER_INT_SEPARATOR = "[^-(?=\\d)]";

Splitting a string by regular expression will be done using the split() method of the String object .
For reference


The split() method splits the string around matches with the given regular expression.


Parsing a regular expression step by step:
  • [^] - any character(s) other than the one that will be in square brackets.
  • [^-] - any characters except dash (-) .
  • x(?=y) - Matches 'x' only if 'x' is followed by 'y'. This is called preemption ( source ).
  • [^-(?=\\d)] - any characters except a dash followed by a numeric character


Then the question is: why does this regular expression also capture a single dash character?

At the output we get:
-1
-
-2233
-3-
-4

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2021-05-03
@saboteur_kiev

x(?=y) - Matches 'x' only if 'x' is followed by 'y'. It's called preemption (source).

This is if this design was freestanding. But you have it enclosed in [], which means
[^] - any character (s) except for the one that will be in square brackets, that is, all characters except -, (, ?, \, d, )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question