Java将字符串拆分为

时间:2017-07-25 02:13:41

标签: java regex algorithm

我有一个大字符串,需要在一行中基于“ - ”拆分成段落,下面是字符串

access.log-2017-07-10   05:30:02    127.0.0.1   GET 200 58944   0.079   -   ""  -   -   -   -   -   /platform/scheduler/logout.jsp  9148257800613007309 -   -   -   -   -   -   -   -   -
access.log-2017-07-10   05:45:00    127.0.0.1   POST    200 10  0.084   "55"    ""  -   -   -   -   -   /platform/scheduler/login.jsp   9148257800613007349 -   -   -   -   -   -   -   -   -
access.log-2017-07-10   05:45:00    127.0.0.1   GET 302 333 0.02    -   "test123"   -   -   -   -   -   /report/scheduler/schedule.jsp?notify=y 9148257800613007349 -   -   -   -   -   -   -   -   -
access.log-2017-07-10   05:45:00    127.0.0.1   GET 200 168427  0.171   -   ""  -   -   -   -   -   /report/reports.jsp?report=6030654077013281451  9148257800613007349 -   -   -   -   -   -   -   -   -
access.log-2017-07-10   05:45:00    127.0.0.1   GET 200 58944   0.034   -   ""  -   -   -   -   -   /platform/scheduler/logout.jsp  9148257800613007349 -   -   -   -   -   -   -   -   -
access.log:2017-07-10   05:59:05    10.233.32.75    POST    302 325 3.189   "133"   ""  -   -   -   -   -   /login.jsp  9148257800613007357 -   -   -   -   -   -   -   -   "http://10.112.251.31:6300/login.jsp?redirectKey=_from_url_e4600ddc-2c02-4c85-9eff-83bee5457c14" kranthi123
access.log-2017-07-10   05:59:06    10.233.32.75    GET 200 30119   1.422   -   ""  -   -   -   -   -   /ncobject.jsp?id=9137288153213290331    9148257800613007357 -   -   -   -   -   -   -   -   "http://10.112.251.31:6300/login.jsp?redirectKey=_from_url_e4600ddc-2c02-4c85-9eff-83bee5457c14"
access.log-2017-07-10   05:59:07    10.233.32.75    GET 200 32  0.009   -   ""  -   -   -   -   -   /CSServlet  9148257800613007357 -   -   -   -   -   -   -   -   "http://10.112.251.31:6300/ncobject.jsp?id=9137288153213290331"
access.log-2017-07-10   05:59:07    10.233.32.75    GET 404 52534   0.454   -   ""  -   -   -   -   -   /scripts/jquery/form-styler/jquery/jquery.formstyler/jquery.formstyler.min.js   9148257800613007357 -   -   -   -   -   -   -   -   "http://10.112.251.31:6300/ncobject.jsp?id=9137288153213290331"
--
start_check_state.log-
start_check_state.log-Initializing WebLogic Scripting Tool (WLST) ...
start_check_state.log-
start_check_state.log:Welcome to WebLogic Server Administration Scripting Shell kranthi123
start_check_state.log-
start_check_state.log-Type help() for help on available commands
start_check_state.log-
start_check_state.log-Connecting to t3://0.0.0.0:6300 with userid system ...
start_check_state.log-Successfully connected to Admin Server 'devSrv25131' that belongs to domain 'dev'.
--
start_check_state.log-
start_check_state.log-Initializing WebLogic Scripting Tool (WLST) ...
start_check_state.log-
start_check_state.log:Welcome to WebLogic Server Administration Scripting Shell kranthi123
start_check_state.log-
start_check_state.log-Type help() for help on available commands
start_check_state.log-
start_check_state.log-Connecting to t3://0.0.0.0:6300 with userid system ...
start_check_state.log-Successfully connected to Admin Server 'devSrv25131' that belongs to domain 'dev'.

我已经在很多方面使用过分裂而且没有一个工作

以下是我用过的一些。

split("\n--\n");
split("^\\s");
split("\\n--\\n")

他们都没有工作

1 个答案:

答案 0 :(得分:2)

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

  • (?m)启用多线模式
  • ^匹配行的开头
  • --匹配字符--
  • $匹配行尾
  • \R?可选择匹配换行符

即。匹配一行只包含两个短划线(-)字符,包括结束该行的换行符(如果存在)。

作为Java代码,即:

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

请参阅IDEONE以获取证明。