且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java将字符串拆分为段落

更新时间:2022-06-14 21:20:45

使用此正则表达式:(?m)^-$ \ R?

  • (?m) Enables multiline mode
  • ^ Match the beginning of a line
  • -- Match the characters --
  • $ Match the end of a line
  • \R? Optionally match a linebreak

即匹配仅由两个破折号(-)字符组成的行,包括以该行结尾的换行符(如果存在)。

I.e. match a line consisting of nothing but two dash (-) characters, including the linebreak ending the line, if present.

作为Java代码,即:

As Java code, that is:

String[] paragraphs = input.split("(?m)^--$\\R?");

请参见 IDEONE 作为证据。