Replace with a value according to the match with a Java regular expression

For example, consider replacing the following SQL string bind parameters with actual values.

insert into table1(c1, c2, c3) values (?, ?, ?)
insert into table1(c1, c2, c3) values (1, 2, 3)
String[] v = {"1", "2", "3"};
		
Pattern p = Pattern.compile("\\?");
Matcher m = p.matcher("insert into table1(c1, c2, c3) values (?, ?, ?)");
		
StringBuffer sb = new StringBuffer();
for (int i=0; m.find(); i++) {
	m.appendReplacement(sb, v[i]);
}
m.appendTail(sb);
System.out.println(sb.toString());

Recommended Posts

Replace with a value according to the match with a Java regular expression
<java> Split the address before and after the street address with a regular expression
[Java] Cut out a part of the character string with Matcher and regular expression
[Java] Change the process according to the situation with the Strategy pattern
Connecting to a database with Java (Part 1) Maybe the basic method
I want to extract between character strings with a regular expression
[Java] How to search for a value in an array (or list) with the contains method
Easy to trip with Java regular expressions
Declare a method that has a Java return value with the return value data type
[Java small story] Monitor when a value is added to the List
I want to ForEach an array with a Lambda expression in Java
How to use Java API with lambda expression
How to use the replace () method (Java Silver)
I tried to break a block with java (1)
Initialize Ruby array with 0 like Java, that is, set the default value to 0
Pass the condition to be used in the Java8 lambda expression filter () as a parameter
(Java) How to implement equals () for a class with value elements added by inheritance
I tried to express the phone number (landline / mobile phone) with a regular expression in Rails and write validation and test
Submit a job to AWS Batch with Java (Eclipse)
[Java] How to get the maximum value of HashMap
How to save a file with the specified extension under the directory specified in Java to the list
[Java] How to execute tasks on a regular basis
HTTPS connection with Java to the self-signed certificate server
Ruby Regular Expression Extracts from a specific string to a string
Dynamically switch writer according to reader value with spring-batch
java regular expression summary
Extract elements by doing regular expression replacement from a lot of HTML with java
[Ruby/Rails] How to generate a password in a regular expression
[Java] How to start a new line with StringBuilder
Replace only part of the URL host with java
Deploy a Java application developed locally with the Cloud Toolkit to an Alibaba Cloud ECS instance
A Java user over a dozen years ago tried to study the functions of Java8 (Lambda expression).
I want to return a type different from the input element with Java8 StreamAPI reduce ()
Display a balloon message in BarButtonItem of NavigationBar with a size according to the amount of text.
How to reduce the load on the program even a little when combining characters with JAVA
I tried to create a java8 development environment with Chocolatey
Assign a Java8 lambda expression to a variable and reuse it
Java program to resize a photo into a square with margins
I tried to modernize a Java EE application with OpenShift.
[Beginner] Try to make a simple RPG game with Java ①
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
[Java] Let's replace data objects with a mapper ~ BeanMapper Orika ~
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
Extract a string starting with a capital letter with a regular expression (Ruby)
Mapping to a class with a value object in How to MyBatis
About the behavior when doing a file map with java
A memorandum to reach the itchy place for Java Gold
A memo to start Java programming with VS Code (2020-04 version)
Be sure to compare the result of Java compareTo with 0
Java to play with Function
Connect to DB with Java
Connect to MySQL 8 with Java
Input to the Java console
How to deal with the type that I thought about writing a Java program for 2 years
Create a simple web server with the Java standard library com.sun.net.httpserver
Try to imitate the idea of a two-dimensional array with a one-dimensional array
A story that I struggled to challenge a competition professional with Java
[Java] Adding an element to the Collection causes a compile error
I want to return to the previous screen with kotlin and java!
A story about hitting the League Of Legends API with JAVA