Java将字符串拆分为段落 [英] Java splitting string into paragraphs

查看:585
本文介绍了Java将字符串拆分为段落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的字符串,需要在一行中基于-将其分成几段,下面是字符串

I have a big string that needs to be split into paragraphs based on "-- " in a single line and below is the string

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的方式很多,但都不起作用

I have used split in so many ways and none of them worked

下面是我使用过的一些。

Below are few of them which i have used.

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

它们都不起作用

推荐答案

使用此正则表达式:(?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 作为证据。

这篇关于Java将字符串拆分为段落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆