Reputation: 87
I have a string as below
when
$Event:com.abc.Event(description == "abc")
then
logger.info("description");
I need to replace the above string with below
when
$Event:com.abc.Event(description == "abc") from entry-point "EventStream"
then
logger.info("description");
In the same way when i encounter
when
$Alarm:com.abc.Alarm(description == "abc")
then
logger.info("alarm description");
i need to change as below
when
$Alarm:com.abc.Alarm(description == "abc") from entry-point "AlarmStream"
then
logger.info("alarm description");
i would like to replace the string using regular expression using greedy match. Please provide me some pointers to acheive the same.
Upvotes: 0
Views: 160
Reputation:
New answer that will use a regex and a test class.
import java.util.Scanner;
public class RegEx {
public static void main(String[] args) {
String text = "when\n$Alarm:com.abc.Alarm(description == \"abc\")\nthen\nlogger.info(\"alarm description\")";
System.out.println(text);
StringBuilder sb = new StringBuilder();
Scanner scan = new Scanner(text);
while(scan.hasNextLine()){
String line = scan.nextLine();
if(line.matches(".*\\.Alarm(.*).*")){
line+=" from entry-point \"AlarmStream\"";
}
sb.append(line+System.getProperty("line.separator"));
}
System.out.println(); // Nicer output
System.out.println(sb.toString());
}
}
The output
when
$Alarm:com.abc.Alarm(description == "abc")
then
logger.info("alarm description")
when
$Alarm:com.abc.Alarm(description == "abc")from entry-point "AlarmStream"
then
logger.info("alarm description")
Upvotes: 0
Reputation:
Easy solution don't bother with regex use Strings method contains instead. Make a Scanner object that parses your string line for line and add the result to a String buffer.
if(line.contains("$Event:com.abc.Event(description == "abc")"){
sb.append(line + "from entry-point \"EventStream\" ");
} else if(line.contains("$Alarm:com.abc.Alarm(description == \"abc\")") {
sb.append(line + "from entry-point \"AlarmStream\" ");
}else {
sb.append(line);
}
Upvotes: 1