Java Regex Help: Splitting String on spaces, “=>”, and commas

前端 未结 3 992
鱼传尺愫
鱼传尺愫 2021-02-05 21:48

I need to split a string on any of the following sequences:

1 or more spaces
0 or more spaces, followed by a comma, followed by 0 or more spaces,
0 or more space

3条回答
  •  感情败类
    2021-02-05 22:24

    Just create regex matching any of your three cases and pass it into split method:

    string.split("\\s*(=>|,|\\s)\\s*");
    

    Regex here means literally

    1. Zero or more whitespaces (\\s*)
    2. Arrow, or comma, or whitespace (=>|,|\\s)
    3. Zero or more whitespaces (\\s*)

    You can replace whitespace \\s (detects spaces, tabs, line breaks, etc) with plain space character if necessary.

提交回复
热议问题