0

I have the below string

String srcString = "String1.String2.String3";

I want to split "srcString" on "."

Using srcString.split(".") is matching all the characters.

What is the regex to match a "." ?

user170008
  • 1,046
  • 6
  • 17
  • 31

3 Answers3

10

In regex dot is special character representing any character except line separator (to also make it match line separators use Pattern.DOTALL flag).

Anyway, use split("\\.")

Explanation:

  • to escape . we can add \ before it so we end up with regex \.
  • now since \ is also special in string literal " " we also need to escape it there, so to express \. we need to write it as "\\.".
Pshemo
  • 122,468
  • 25
  • 185
  • 269
3

Use split("\\.") as . (dot) is special character so use \\ before .(dot)

Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37
0

You could also call the function org.apache.commons.lang.StringUtils.split(String, char) of the library commons-lang [http://commons.apache.org/lang/][1]

M. Abbas
  • 6,409
  • 4
  • 33
  • 46