1

I have a string as below.

$Trap:com.oss.Event(description  matches  "abc") 

In the above string the generic portions is $Trap:com.oss.Event(<Any string>).

when i encounter the above string i need to replace with the below string.

$Trap:com.oss.Event(description  matches  "abc") from entry-point "EventStream".

To achieve the above i am using the following logic in java.

String stmt= $Trap:com.oss.Event(description  matches  "abc")

if(stmt.matches(".*\\.Event(.*).*")&& s.equals(""))
{
                stmt+=" from entry-point "+"ABCStream";
}

But the above logic is not working as expected when the string is as below.

stmt="$Trap:com.oss.Event(description matches "abc") or $Trap:com.oss.Event(description matches  "cde");

I need to generate the following corresponding string using regular expression.

$Trap:com.oss.Event(description matches "abc") from entry-point "ABCStream" or $Trap:com.oss.Event(description matches  "cde") from entry-point "ABCStream"

Please provide some pointers to achieve the same.

1
  • 2
    can't you just use String.Replace? Commented Sep 29, 2011 at 15:11

2 Answers 2

2

This do the stuff:

public class A {
    public static void main(String[] args) {
        String stmt="$Trap:com.oss.Event(description matches \"abc\") or $Trap:com.oss.Event(description matches  \"cde\")";
        System.out.println(stmt);
        System.out.println(stmt.replaceAll("(\\$Trap:com.oss.Event\\([^)]*\\))", "$1 from entry-point \"ABCStream\""));
    }
}

As you can see, you must double escape some symbols. The first and last parenthesis are for grouping the regex, and you can print that group with "$1"

And procuded this output:

$Trap:com.oss.Event(description matches "abc") or $Trap:com.oss.Event(description matches  "cde")
$Trap:com.oss.Event(description matches "abc") from entry-point "ABCStream" or $Trap:com.oss.Event(description matches  "cde") from entry-point "ABCStream"
Sign up to request clarification or add additional context in comments.

Comments

2

The following Java code works:

String stmt = "$Trap:com.oss.Event(description matches \"abc\")"
   + " or $Trap:com.oss.Event(description matches  \"cde\")";

Pattern p = Pattern.compile(".*?\\.Event\\(.*?\\)");
String res = p.matcher(stmt).replaceAll("$0 from entry-point \"ABCStream\"");

System.err.println(res);

and produces the following result:

$Trap:com.oss.Event(description matches "abc") from entry-point "ABCStream" or $Trap:com.oss.Event(description matches  "cde") from entry-point "ABCStream"

You need to quote special chars, e.g. round brackets, inside regexp and also use lazy match, i.e. *?. The $0 refers to the found matching group.

Using Pattern.compile() will save you some performance, if you are doing this replacement repeatedly.

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.