1

I want to replace all :variable (word starting with :) with ${variable}$.

For example,

  • :aks_num with ${aks_num}$

  • :brn_num with ${brn_num}$

Following is my code, which does not work:

public static void main(String[] argv) throws Exception 
{
    CharSequence chSeq = "AND ((:aks_num = -1) OR (aks_num = :aks_num AND ((:brn_num = -1) OR (brn_num = :brn_num))))";

    // replaceAll also not working
    //String s = chSeq.replaceAll(":\\([a-z_]*\\)","\\${ $1 \\}$");

    Pattern p = Pattern.compile(":\\([a-z_]*\\)");
    Matcher m = p.matcher(chSeq);

    if (m.find()) {
         System.out.println("Found value: " + m.group(0) );
         System.out.println("Found value: " + m.group(1) );
         System.out.println("Found value: " + m.group(2) );
      } else {
         System.out.println("NO MATCH");
      }
}

While in shell script the following regex works perfectly:

s/:\([a-z_]*\)/${\1}$/g
6
  • Pattern p = Pattern.compile("\\(:[a-z_]*"); Commented Apr 11, 2016 at 11:28
  • 2
    ReplaceAll works - s.replaceAll(":([a-z_]+)", "\\${$1}\\$") Commented Apr 11, 2016 at 11:28
  • regex101.com/r/kV0tB2/2 Commented Apr 11, 2016 at 11:32
  • Thanks @WiktorStribiżew Commented Apr 11, 2016 at 11:36
  • @praxnet: I posted an answer below with explanations and more ideas on the enhancements. Commented Apr 11, 2016 at 11:38

4 Answers 4

2

:\\([a-z_]*\\) (with escaped parenthesis) means that you want to match expressions like :(aks_num). Obviously, there are no such expression in the input string. That explains why there are no matches.

Instead, if you want to use parenthesis in order to capture some variables, you should not escape the parenthesis.

Example :

CharSequence chSeq = "AND ((:aks_num = -1) OR (aks_num = :aks_num AND ((:brn_num = -1) OR (brn_num = :brn_num))))";
Pattern p = Pattern.compile(":([a-z_]*)");
Matcher m = p.matcher(chSeq);

while (m.find()) {
  System.out.println("Found value: " + m.group(0)+". Captured : "+m.group(1));
}

Output:

Found value: :aks_num. Captured : aks_num
Found value: :aks_num. Captured : aks_num
Found value: :brn_num. Captured : brn_num
Found value: :brn_num. Captured : brn_num
Sign up to request clarification or add additional context in comments.

1 Comment

@Shafizadeh some golds are easy to get but it is not my goal :)
2
CharSequence chSeq = "AND ((:aks_num = -1) OR (aks_num = :aks_num AND ((:brn_num = -1) OR (brn_num = :brn_num))))";

// replaceAll also not working
//String s = chSeq.replaceAll(":\\([a-z_]*\\)","\\${ $1 \\}$");

Pattern p = Pattern.compile(":(\\w+)");
Matcher m = p.matcher(chSeq);

while (m.find()) {
     System.out.println("Found value: " + m.group(1) );
}

Ideone Demo

Working fine with replaceAll

Pattern p = Pattern.compile("(:\\w+)");
Matcher m = p.matcher(x);
x = m.replaceAll("\\${$1}\\$");

Comments

1

You don't need to escape the parentheses, so

Pattern.compile(":([a-z_]*)");

should work.

Comments

1

I believe you got confused with the Java's regex syntax that is different from regular sed syntax. You do not need to escape parentheses to make them "special" grouping operators. Vice versa, in Java, when you escape parentheses, they start matching literal ( and ) symbols.

In the replacement pattern, $ must be escaped for the regex engine to replace with literal $ symbols, but you do not need to escape braces there.

So, just use

.replaceAll(":([a-z_]+)", "\\${$1}\\$")

See the IDEONE demo

I suggest the + quantifier because I doubt you need to match a : followed with a space, or digits - any non-letter.

BTW, you do not need any /g flag in Java since replaceAll will replace all matches with the provided replacement pattern.

NOTE: you can further adjust the pattern to match all letters/digits/underscores with ":(\\w+)". Or just alphanumerics/underscore: ":([\\p{Alnum}_]+)".

2 Comments

@Shafizadeh: You are thinking this up, see s/:\([a-z_]*\)/${\1}$/g
Yes, maybe just a-z is up for OP. Anyway I think your NOTE is enough.

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.