我试图在一个完整的行中打破基于字符串“ - ”的段落,该段落如下所示。如果一行仅包含“ - ”,我必须断开,因为我有一些其他限制,所以我不能基于行分割。
Hi Hello this is -- to test
--
java split function -- test
预期产出。
["Hi Hello this is -- to test", "java split function -- test"]
我尝试了下面的代码并且它无法正常工作
searchValue.split("--")
和
searchValue.split("\n--\n")
他们两个都没有工作。并在一行中分割所有“ - ”
在这种情况下我应该如何编写分割
确切的输入字符串:
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
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 '' that belongs to domain 'dev'.
--
cia.log.2017-07-20-10.07.2017 6:00:05.963 [INFO ] <CIA> - <session.CIADispatcherBean> - <Flow_Id Alarm_Id/NC_Id : JOB PROCESS> <EventType : Activate Maintenance Ticket> - [event :alarmKey=JOB PROCESS,srcSystem=CIA Planned Maintenance]
cia.log.2017-07-20-10.07.2017 6:00:05.965 [INFO ] <CIA> - <em.CIAEventQueue> - <Flow_Id Alarm_Id/NC_Id : JOB PROCESS> <EventType : Activate Maintenance Ticket> - COMPLETED
cia.log.2017-07-20-10.07.2017 6:00:06.039 [INFO ] <CIA> - <messagelisteners.MessageListenerDelegate> - <Flow_Id Alarm_Id/NC_Id : JOB PROCESS> <EventType : Close Maintenance Ticket> - message handling started. sequence = [-1115362510203642553] node =
cia.log.2017-07-20-10.07.2017 6:00:06.040 [INFO ] <CIA> - <dispatcher.MDBProcessor> - <Flow_Id Alarm_Id/NC_Id : JOB PROCESS> <EventType : Close Maintenance Ticket> - CIA Event <JOB PROCESS> processing on <>
cia.log.2017-07-20-10.07.2017 6:00:06.043 [INFO ] <CIA> - <dispatcher.MDBProcessor> - <Flow_Id Alarm_Id/NC_Id : JOB PROCESS> <EventType : Close Maintenance Ticket> - CIA Event <JOB PROCESS> was successfully processed on <>
cia.log.2017-07-20:10.07.2017 6:00:06.044 [INFO ] <CIA> - <messagelisteners.MessageListenerDelegate> - <Flow_Id Alarm_Id/NC_Id : JOB PROCESS> <EventType : Close Maintenance Ticket> - message processed. sequence = [-1115362510203642553] node =
cia.log.2017-07-20-10.07.2017 6:00:06.058 [INFO ] <CIA> - <messagelisteners.MessageListenerDelegate> - <Flow_Id Alarm_Id/NC_Id : JOB PROCESS> <EventType : Activate Maintenance Ticket> - message handling started. sequence = [-1115362510203642553] node = test123
cia.log.2017-07-20-10.07.2017 6:00:06.060 [INFO ] <CIA> - <dispatcher.MDBProcessor> - <Flow_Id Alarm_Id/NC_Id : JOB PROCESS> <EventType : Activate Maintenance Ticket> - CIA Event <JOB PROCESS> processing on <>
cia.log.2017-07-20-10.07.2017 6:00:06.063 [INFO ] <CIA> - <dispatcher.MDBProcessor> - <Flow_Id Alarm_Id/NC_Id : JOB PROCESS> <EventType : Activate Maintenance Ticket> - CIA Event <JOB PROCESS> was successfully processed on <>
cia.log.2017-07-20-10.07.2017 6:00:06.064 [INFO ] <CIA> - <messagelisteners.MessageListenerDelegate> - <Flow_Id Alarm_Id/NC_Id : JOB PROCESS> <EventType : Activate Maintenance Ticket> - message processed. sequence = [-1115362510203642553] node =
--
cpm_.log-2017-07-10 05:27:53 INFO com..solutions.tfnurg.toms.cpm.job.BorrowedEquipmentNotificationJob :29 - Job for creating notification tasks started.
cpm_.log-2017-07-10 05:27:53 INFO com..solutions.tfnurg.toms.cpm.bean.impl.test123 :471 - All required remind tasks created without any errors
cpm_.log:2017-07-10 05:27:53 INFO com..solutions.tfnurg.toms.cpm.job.BorrowedEquipmentNotificationJob :46 - Created Notification Tasks:
cpm_.log-2017-07-10 05:27:53 INFO com..solutions.tfnurg.toms.cpm.job.BorrowedEquipmentNotificationJob :34 - Job for creating notification tasks ended.
--
答案 0 :(得分:4)
您应该搜索\n
(换行符)和/或\r
(回车)。
试试这个正则表达式:
[\n\r]+--[\n\r]+
DEMO:https://regex101.com/r/huPete/1
在Java中:
searchValue.split("[\\n\\r]+--[\\n\\r]+")
答案 1 :(得分:1)
这对我来说很好,也许有其他答案所暗示的奇怪的空白&amp;评价。
这是一个演示您希望输入和输出的演示:https://ideone.com/1vKnrm
输入:
import java.util.Arrays;
/**
* https://stackoverflow.com/questions/45276416/java-split-not-working-as-expected-for-string
*/
class JavaSplitTest
{
public static void main(String[] args)
{
String searchValue = "Hi Hello this is -- to test\n" +
"\n" +
"--\n" +
"\n" +
"java split function -- test\n";
String[] splitted = searchValue.split("\n--\n");
System.out.println(Arrays.asList(splitted));
}
}
输出:
[Hi Hello this is -- to test
,
java split function -- test
]