Reputation: 143
I am trying to break a paragraph based on the string "--" in a complete line and the paragraph looks like below. i have to break if a line contains only "--" only i cannot split based on line as i have some other limitation.
Hi Hello this is -- to test
--
java split function -- test
Expected output.
["Hi Hello this is -- to test", "java split function -- test"]
i tried the below code and it did not work
searchValue.split("--")
and
searchValue.split("\n--\n")
both of them have not worked. and is splitting for all "--" in a line
any idea how i should write the split in this case
Exact input string:
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.
--
Upvotes: 0
Views: 143
Reputation: 15465
This works fine for me, perhaps there is strange whitespace as suggested by the other answers & comments.
Here's a demo showing the input and output you hoped for: https://ideone.com/1vKnrm
Input:
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));
}
}
Output:
[Hi Hello this is -- to test
,
java split function -- test
]
Upvotes: 1
Reputation: 31851
You should search for both \n
(newline) and/or \r
(carriage return).
Try this regex:
[\n\r]+--[\n\r]+
DEMO: https://regex101.com/r/huPete/1
In Java:
searchValue.split("[\\n\\r]+--[\\n\\r]+")
Upvotes: 4