pattern | Match string |
---|---|
XYZ | The string XYZ |
[XYZ] | One character of XYZ |
[^XYZ] | One character other than XYZ |
[X-Z] | X~One letter in the range of Y |
[a-zA-Z] | Range from a to z or A to Z |
(X|Y|Z) | One of XYZ [ABC|] |
X* | X occurs 0 or more times ("do*n"in the case of"dn","don","dooon"Such) |
X+ | X occurs more than once ("do+n"in the case of"don","dooon"Such) |
X? | X occurs 0 or 1 times ("do?n"in the case of"dn"Or"don") |
X{n} | X occurs n times ("do{2}n"in the case of"doon") |
X{n,} | X occurs n times or more ("do{2,}n"in the case of"doon","doooon"Such) |
X{n,m} | X occurs n to m times ("do{2,3}n"in the case of"doon"Or"dooon") |
. | Any character |
\w | uppercase letter/Lowercase alphanumeric characters, underscore [a-zA-Z_0-9] |
\d | Numbers [0-9] |
\D | Other than numbers [^0-9] |
\s | Blank [ \t\n\x0B\f\r] |
^ | Match at the beginning of a line |
$ | Line tail match |
\b | Word boundaries |
\\ | Match backslash |
\n | Match newline character |
\t | Match tab character |
^\d{10}$ | 10 single-byte numbers |
^\d{5,10}$ | Half-width number 5 digits or more and 10 digits or less |
\d{2,4}-\d{2,4}-\d{4} | phone number (Half-width number 2~3 digits-Half-width number 2~3 digits-4 single-byte numbers) |
^\d{3}-\d{4}$ | Postal code (3 single-byte numbers-4 single-byte numbers) |
[1]+$ | One-digit or more half-width alphanumeric characters (0-9、a-z、A-Z) |
//Sample 1 Phone/Zip code match
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
//Character string to judge
String str = "012-345-6789";
//Generate a pattern to judge
Pattern p = Pattern.compile("\\d{2,4}-\\d{2,4}-\\d{4}"); //phone number
//Pattern p = Pattern.compile("^\\d{3}-\\d{4}$"); //Postal code
Matcher m = p.matcher(str);
System.out.println(m.find()); //true
}
}
//Sample 2 forward/Rear/Partial Match
public class Main {
public static void main(String[] args) {
//Character string to judge
String str = "000012-345-6789";
//Generate a pattern to judge
//Pattern p = Pattern.compile("\\d{2,4}-\\d{2,4}-\\d{4}.*"); //Phone number prefix match
//Pattern p = Pattern.compile(".*\\d{2,4}-\\d{2,4}-\\d{4}"); //Phone number suffix match
Pattern p = Pattern.compile(".*\\d{2,4}-\\d{2,4}-\\d{4}.*"); //Phone number partial match
Matcher m = p.matcher(str);
System.out.println(m.find()); //true
}
}
while (match.find ())
until the find method becomes false, and get the start position (start), end position (end), and match character string (group) of the string.group [0]
: entire matched stringgroup [1]
: First element of the submatch stringimport java.util.*;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
var str = "Mobile is 0123-99-It's 0000. Home is 000-123-4567 and so on.";
var ptn = Pattern.compile("(\\d{2,4})-(\\d{2,4})-(\\d{4})");
var match = ptn.matcher(str);
while (match.find()) {
System.out.println("Starting position:" + match.start());
System.out.println("End position:" + match.end());
System.out.println("Matching string:" + match.group());
System.out.println("Area code:" + match.group(1));
System.out.println("City code:" + match.group(2));
System.out.println("Subscriber number:" + match.group(3));
System.out.println("-----");
}
}
}
CASE_INSENSITIVE
import java.util.*;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
var str = "For work is [email protected] is com. NEKO for private [email protected] is com.";
var ptn = Pattern.compile("[a-z0-9.!#$%&'*+/=?^_{|}~-]+@[a-z0-9-]+(\\.[a-z0-9-]+)*", Pattern.CASE_INSENSITIVE);
var match = ptn.matcher(str);
while (match.find()) {
System.out.println(match.group());
}
}
}
MULTILINE
^
and $
^
matches not only the beginning but also the number after the line break$
, it also matches the end of the lineimport java.util.*;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
var str = "Friends in the first grade\I wonder if n100 people can do it\n";
// var ptn = Pattern.compile("^\\d*");
var ptn = Pattern.compile("^\\d*", Pattern.MULTILINE);
var match = ptn.matcher(str);
while (match.find()) {
System.out.println(match.group()); //1 100
}
}
}
DOTALL
.
\ n
)import java.util.*;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
var str = "Wanted to meet\nWanted to meet\nWanted to meet\nYES";
// var ptn = Pattern.compile("^.+");
var ptn = Pattern.compile("^.+", Pattern.DOTALL);
var match = ptn.matcher(str);
while (match.find()) {
System.out.println(match.group());
//Wanted to meet
//Wanted to meet
//Wanted to meet
//YES
}
}
}
var ptn = Pattern.compile("(?i)[a-z0-9.!#$%&'*+/=?^_{|}~-]+@[a-z0-9-]+(\\.[a-z0-9-]+)*");
//var ptn = Pattern.compile("[a-z0-9.!#$%&'*+/=?^_{|}~-]+@[a-z0-9-]+(\\.[a-z0-9-]+)*", Pattern.CASE_INSENSITIVE);
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
var tags = "<p><strong>NEKO</strong>site<a href='index.html'><img src='cat.jpg' /></a></p>";
//Longest match
//var ptn = Pattern.compile("<.+>"); //<p><strong>NEKO</strong>site<a href='index.html'><img src='cat.jpg' /></a></p>
//Shortest match
var ptn = Pattern.compile("<.+?>");
var match = ptn.matcher(tags);
while (match.find()) {
System.out.println(match.group());
//<p>
//<strong>
//</strong>
//<a href='index.html'>
//<img src='cat.jpg' />
//</a>
//</p>
}
}
}
group ("name ")
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
var msg = "Mobile is 0123-99-It's 0000. Home is 000-123-4567 and so on.";
var ptn = Pattern.compile("(?<area>\\d{2,4})-(?<city>\\d{2,4})-(?<local>\\d{4})");
var match = ptn.matcher(msg);
while (match.find()) {
System.out.println("Starting position:" + match.start());
System.out.println("End position:" + match.end());
System.out.println("Matching string:" + match.group());
System.out.println("Area code:" + match.group("area"));
System.out.println("City code:" + match.group("city"));
System.out.println("Subscriber number:" + match.group("local"));
System.out.println("-----");
}
}
}
\\ 1
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
//Back reference
public static void main(String args[]){
String str1 = "My name is <div>Neko</div>";
String str2 = "I am a <span>Cat</span>";
String str3 = "<span>Hello World</div>";
String regex = "<(div|span)>.*?<\\/\\1>";
Pattern p = Pattern.compile(regex);
System.out.println("pattern: " + regex);
check(p, str1);
check(p, str2);
check(p, str3);
}
private static void check(Pattern p, String target){
Matcher m = p.matcher(target);
if (m.find()){
System.out.println("Match! " + target);
}else{
System.out.println("Unmatch! " + target);
}
}
}
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
private static void match(Pattern ptn, String input) {
var match = ptn.matcher(input);
while (match.find()) {
System.out.println(match.group());
}
System.out.println("---");
}
public static void main(String[] args) {
var re1 = Pattern.compile("My(?=Yes)");
var re2 = Pattern.compile("My(?!Yes)");
var re3 = Pattern.compile("(?<=。)My");
var re4 = Pattern.compile("(?<!。)My");
var msg1 = "My yes is a cat";
var msg2 = "I am a cat. My name isn't there yet.";
match(re1, msg1); //My
match(re1, msg2); //---
match(re2, msg1); //---
match(re2, msg2); //My,My,My
match(re3, msg1); //---
match(re3, msg2); //My
match(re4, msg1); //My
match(re4, msg2); //My,My
}
}
$ {"name "}
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
var str = "Click here for inquiries https://www.neko.com/is.";
System.out.println(str.replaceAll(
"(?i)http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\\\w ./?%&=-]*)?",
"<a href=\"$0\">$0</a>"));
//Contact Us<a href="https://www.neko.com/">https://www.neko.com/</a>is.
}
}
import java.util.regex.Pattern;
public class Main {
//One or more digits+Split with
public static void main(String[] args) {
var str = "There are 2 chickens in the backyard and 22 chickens in the backyard.";
var re = Pattern.compile("\\d{1,}Wow");
var result = re.split(str);
System.out.println(String.join(" ", result));
//There is a chicken in the backyard
}
}
0-9a-zA-Z ↩︎
Recommended Posts