0

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.

0

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the update.But the problem in my case is ".Event(" and ")" in the first case and ".Alarm(" and ")" will be constant and the remain portion will change in when portion so i dont want to go for exact string comparison.
@gsr I have made a new answer for you check out if that fulfills your requirements.
0

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.