-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path1-replace.java
More file actions
36 lines (32 loc) · 1013 Bytes
/
1-replace.java
File metadata and controls
36 lines (32 loc) · 1013 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Example of using regular expressions for replacing strings.
*/
final class Replace {
/**
* Private constructor prevents class from being instantiated.
*/
private Replace() {
}
/**
* Entry point of the program.
*
* @param args command-line arguments, not used
*/
public static void main(final String[] args) {
//Using String API
String regularExpression1 = "abc";
String input1 = "abcdefg";
String output1 = input1.replaceAll(regularExpression1, "123");
System.out.println(output1);
// Using regex API
String regularExpression2 = "abc";
String input2 = "abcdefgABCDEFG";
Pattern pattern = Pattern.compile(regularExpression2,
Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input2);
String output2 = matcher.replaceAll("789");
System.out.println(output2);
}
}