Find and replace java regex

I am trying to find environment variables in the input and replace them with values.

The sample env variable is set to ${\\.}

 Pattern myPattern = Pattern.compile( "(${\\.})" ); String line ="${env1}sojods${env2}${env3}"; 

How to replace env1 with 1 and env2 with 2 and env3 with 3 , so what after that I will have a new line 1sojods23 ?

+10
source share
7 answers

Strings in Java are immutable, which makes this a bit complicated if you are talking about an arbitrary number of things that you need to find and replace.

In particular, you need to define your replacements in Map , use StringBuilder (before Java 9 you should use a less efficient StringBuffer ) and the appendReplacements() and appendTail() methods from Matcher . The final result will be saved in your StringBuilder (or StringBuffer ).

 Map<String, String> replacements = new HashMap<String, String>() {{ put("${env1}", "1"); put("${env2}", "2"); put("${env3}", "3"); }}; String line ="${env1}sojods${env2}${env3}"; String rx = "(\\$\\{[^}]+\\})"; StringBuilder sb = new StringBuilder(); //use StringBuffer before Java 9 Pattern p = Pattern.compile(rx); Matcher m = p.matcher(line); while (m.find()) { // Avoids throwing a NullPointerException in the case that you // Don't have a replacement defined in the map for the match String repString = replacements.get(m.group(1)); if (repString != null) m.appendReplacement(sb, repString); } m.appendTail(sb); System.out.println(sb.toString()); 

Exit:

 1sojods23 
+44
source

I know this is old, I myself was looking for an example appendReplacement/appendTail when I found it; However, the OP question does not need the complex multi-line solutions that I saw here.

In this exact case, when the replaced string saves the value that we want to replace, then this can be done easily with replaceAll :

 String line ="${env1}sojods${env2}${env3}"; System.out.println( line.replaceAll("\\$\\{env([0-9]+)\\}", "$1") ); // Output => 1sojods23 

Demo

When a replacement is random based on some conditions or logic for each match, you can use appendReplacement/appendTail for example

+8
source

This gives you 1sojods23 :

 String s = "${env1}sojods${env2}${env3}"; final Pattern myPattern = Pattern.compile("\\$\\{[^\\}]*\\}"); Matcher m = myPattern.matcher(s); int i = 0; while (m.find()) { s = m.replaceFirst(String.valueOf(++i)); m = myPattern.matcher(s); } System.out.println(s); 

and this also works:

 final String re = "\\$\\{[^\\}]*\\}"; String s = "${env1}sojods${env2}${env3}"; int i = 0; String t; while (true) { t = s.replaceFirst(re, String.valueOf(++i)); if (s.equals(t)) { break; } else { s = t; } } System.out.println(s); 
+3
source

Hope you find this code useful:

  Pattern phone = Pattern.compile("\\$\\{env([0-9]+)\\}"); String line ="${env1}sojods${env2}${env3}"; Matcher action = phone.matcher(line); StringBuffer sb = new StringBuffer(line.length()); while (action.find()) { String text = action.group(1); action.appendReplacement(sb, Matcher.quoteReplacement(text)); } action.appendTail(sb); System.out.println(sb.toString()); 

Expected yield: 1sojods23 .

+2
source

You can use StringBuffer in combination with the Matcher appendReplacement () method, but if the template does not match, there is no point in creating a StringBuffer.

For example, here is a pattern that matches $ {...}. Group 1 is the content between braces.

 static Pattern rxTemplate = Pattern.compile("\\$\\{([^}\\s]+)\\}"); 

And here is an example function that uses this template.

 private static String replaceTemplateString(String text) { StringBuffer sb = null; Matcher m = rxTemplate.matcher(text); while (m.find()) { String t = m.group(1); t = t.toUpperCase(); // LOOKUP YOUR REPLACEMENT HERE if (sb == null) { sb = new StringBuffer(text.length()); } m.appendReplacement(sb, t); } if (sb == null) { return text; } else { m.appendTail(sb); return sb.toString(); } } 
0
source
  Map<String, String> replacements = new HashMap<String, String>() { { put("env1", "1"); put("env2", "2"); put("env3", "3"); } }; String line = "${env1}sojods${env2}${env3}"; String rx = "\\$\\{(.*?)\\}"; StringBuffer sb = new StringBuffer(); Pattern p = Pattern.compile(rx); Matcher m = p.matcher(line); while (m.find()) { // Avoids throwing a NullPointerException in the case that you // Don't have a replacement defined in the map for the match String repString = replacements.get(m.group(1)); if (repString != null) m.appendReplacement(sb, repString); } m.appendTail(sb); System.out.println(sb.toString()); 

In the above example, we can use the card with only the key and the values ​​--keys can be env1, env2 ..

0
source

Using groups after matching ${env1} will be your first group, and then you will use the regular expression to replace what is in each group.

 Pattern p = Pattern.compile("(${\\.})"); Matcher m = p.matcher(line); while (m.find()) for (int j = 0; j <= m.groupCount(); j++) //here you do replacement - check on the net how to do it;) 
-1
source

Source: https://habr.com/ru/post/910190/


All Articles