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,016
  • 6
  • 17
  • 29

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
  • 118,400
  • 24
  • 176
  • 257
3

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

Pramod Kumar
  • 7,696
  • 5
  • 27
  • 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,219
  • 4
  • 31
  • 41