Daniel Dai
Daniel Dai

Reputation: 63

Java split() method can not distinguish "." and "-" on Windows

For example:

"xxxxx-ex-xxxxx.ex-xxxx".split(".ex")

The results would be "xxxxx", "-xxxxx" and "-xxxx", that means the "-ex" has been identified as ".ex".

Of course, "\.ex" works well in this case. I just wondering why does it happened? Seems like it only occurred on Windows.

Upvotes: 0

Views: 116

Answers (1)

When invoking "xxxxx-ex-xxxxx.ex-xxxx".split(".ex") you're invoking java.lang.String.split(). This method accepts ".ex" as a regex (Regular Expression).

We can use RegExr to learn about regex and see why the -ex part is also identified. I inputted your string and regex into the website already: https://regexr.com/4rq3o

From that tool we can see that the dot . is special and means match any character, so by typing ".ex" it means find a pattern where any character followed by ex is present.

In order to treat ".ex" as a literal string, we need to escape the dot. As you discovered, \.ex does the trick because back-slash is an escape character.

Upvotes: 3

Related Questions