40-regular expression

Regular expression

  Through a series of analysis, it is found that String is a very versatile type, because String can not only support string processing operations, but also supports conversion to various data types. Therefore, in project development, as long as the information input by the user is basically Both are represented by String. Therefore, when converting to other data types, in order to ensure the correctness of the conversion, complex verification processing is required. It is very troublesome to rely solely on the methods in the String class.

Know regular expressions

  Suppose there is a character string, and it is required to determine whether it is composed of numbers. If it is composed of numbers, it will be converted into a number for multiplication calculation.
String to number

public class RegexStudy {
    
    
	public static void main(String[] args) {
    
    
		String str = "1234";
		if(isNumber(str)) {
    
    
			int num = Integer.parseInt(str);
			System.err.println(num *2);
		}
	}
	public static boolean isNumber(String str) {
    
    
		char data [] = str.toCharArray();
		for (int x=0; x<data.length; x++) {
    
    
			if(data[x] >'9' || data[x]<'0') {
    
    
				return false;
			}
		}
		return true;
	}
}

In fact, this verification function is very simple, but it requires developers to write a lot of code to achieve. In this case, it is best to use regular expressions to achieve;
use regular expressions to achieve

public class RegexStudy {
    
    
	public static void main(String[] args) {
    
    
		String str = "1234";
		if(str.matches("\\d+")) {
    
    
			int num = Integer.parseInt(str);
			System.out.println(num * 2);
		}
	}
}

  Regular expressions were originally developed from the Perl language. Regular expressions have been supported since JDK1.4, and the java.util.regex development kit is provided. At the same time, the String class has also been modified to have a method to directly support it. Regular processing.
  The biggest feature of using regular expression is that it is convenient to carry outVerification processing, And facilitate the modification of complex strings.

Commonly used regular mark (remember)

  If you want to perform regular processing operations, you must first master the commonly used regular marks. Starting from JDK1.4, the java.util.regex development kit provides the Pattern program class, which defines all the supported regular marks.

  • [Quantity: Single] Character matching:
    • Arbitrary characters: means composed of arbitrary characters;
    • \\:match"\";
    • \n: match newline;
    • \t: match a tab character;
  • [Quantity: Single] Character set matching (you can choose a character from it)
    • [abc]: It may be any of the letters abc;
    • [^abc]: Not any of the letters abc;
    • [a-zA-Z]: Consists of an arbitrary letter, not case sensitive;
    • [0-9]: means consisting of one digit;
  • [Quantity: Single] Simplified character set:
    • .: Represents any character;
    • \d: equivalent to "[0-9]";
    • \D: equivalent to "[^0-9]";
    • \s: Match any one space, which may be a space, a new line or a tab character;
    • \S: match any non-space data;
    • \w: match letters, numbers, and underscores, equivalent to "[a-zA-Z_0-9]";
    • \W: match non-letters, numbers, and underscores, equivalent to "[^a-zA-Z_0-9]";
  • Boundary matching:
    • ^: the beginning of the matching boundary;
    • $: start of matching boundary;
  • The quantity means that by default, only the quantity unit is added to match multiple characters:
    • Expression?: The regular can appear 0 or 1 times;
    • Expression*: The regular can appear 0 times or 1 time or multiple times;
    • Expression +: the regular can appear 1 or more times;
    • Expression {n}: The length of the expression is exactly n times;
    • Expression {n,}: The length of the expression is more than n times;
    • Expression {n,m}: The length of the expression is n~m times;
  • Logical expression: multiple regulars can be connected:
    • Expression X expression Y: X expression immediately followed by Y expression;
    • Expression X|Expression Y: only one expression satisfies;
    • (Expression X): Set an overall description for expression X, and you can set the number unit for the overall description.

String class support for regular

  In most cases of regular expression processing, it will be completed based on the String class, and the following regular-related operation methods are provided in String:

Method name Types of description
public boolean matches(String regex) ordinary Regularly judge the specified string
public String replaceAll(String regex, String replacement) ordinary Replace all
public String replaceFirst(String regex, String replacement) ordinary Replace the first
public String[] split(String regex) ordinary String regular split
public String[] split(String regex, int limit) ordinary Split into specified number

Example: string replacement (remove non-letters and numbers)

public class RegexStudy {
    
    

	public static void main(String[] args) {
    
    
		String str = "SERTB4W@#$%^56Y43%$^Y3Q4$$5WERFEWG,&^LSDGPSDFG";		//要判断的数据
		String regex = "[^a-zA-Z0-9]+";		//正则表达式
		System.out.println(str.replaceAll(regex, ""));
	}
}

Example: String split

public class RegexStudy {
    
    
	public static void main(String[] args) {
    
    
		String str = "a12312312aa23423423n2bnn23n4n23";		//要判断的数据
		String regex = "\\d+";		//正则表达式
		String [] result = str.split(regex);
		for(int x=0; x<result.length; x++) {
    
    
			System.out.println(result[x]);
		}
	}
}

It is relatively easy to split and replace during regular processing, but the more troublesome part is the data verification part.
Example: Determine whether a piece of data is a decimal, and if so, change it to a double type

public class RegexStudy {
    
    

	public static void main(String[] args) {
    
    
		String str = "100.32";		//要判断的数据
		String regex = "\\d+(\\.\\d+)?";		//正则表达式
		System.out.println(str.matches(regex));
	}
}

Example: Determine whether a string is composed of dates, and if so, change it to Date type

import java.text.SimpleDateFormat;

public class RegexStudy {
    
    

	public static void main(String[] args) throws Exception {
    
    
		String str = "1981-12-12";		//要判断的数据
		String regex = "\\d{4}-\\d{2}-\\d{2}";		//正则表达式
		if(str.matches(regex)) {
    
    
			System.out.println(new SimpleDateFormat("yyyy-MM-dd").parse(str));
		};
	}
}

Regular expressions cannot judge the content, only the format.
Example: Determine whether the given phone number is correct

public class RegexStudy {
    
    

	public static void main(String[] args) throws Exception {
    
    
		//号码:5799033"\\d{7,8}"
		//04515799033"(\\d{3,4})?\\d{7,8}"
		//(0451)-5799033
		String str = "(0451)-5799033";		//要判断的数据
		String regex = "((\\d{3,4})|(\\(\\d{3,4}\\)-))?\\d{7,8}";		//正则表达式
		System.out.println(str.matches(regex));
	}
}

Now that you can use regular rules for verification, you can use it to implement an email address format verification.
Example: Verify email format
rre3LT.png

public class RegexStudy {
    
    

	public static void main(String[] args) throws Exception {
    
    
		//email用户名可以由字母、数字、_所组成(不应使用_开头)
		//email域名可以由字母、数字、_、-所组成
		//email后缀为.cn、.com、.net、.com.cn、.gov
		String str = "[email protected]";		//要判断的数据
		String regex = "[a-zA-Z0-9]\\w+@\\w+\\.(cn|com|nwt|gov|com\\.cn)";		//正则表达式
		System.out.println(str.matches(regex));
	}
}

  Now these types of regular matching processing operations are the most common process.

java.util.regex package support

  Although you can use the String class to implement regular operations in most cases, there are also cases where you need to use the regular processing classes provided in the java.util.regex development kit. Two classes are provided in this class: regular expression compilation class: Pattern; regular matching class: Matcher;

  • The Pattern class provides support for the compilation and processing of regular expressions:public static Pattern compile(String regex);
    • It also provides string splitting support:public String[] split(CharSequence input);
import java.util.regex.Pattern;

public class RegexStudy {
    
    

	public static void main(String[] args) throws Exception {
    
    
		String str = "adsgsdfg()2#$%^dfgh#%^DFh2345DFH@$#";		//要判断的数据
		String regex = "[^a-zA-Z0-9]+";		//正则表达式
		Pattern pat = Pattern.compile(regex);
		String reslut[] = pat.split(str);
		for(int x=0;x<reslut.length;x++) {
    
    
			System.out.println(reslut[x]);
		}
	}
}
  • Matcher class: a processing class that implements regular matching. The instantiation of objects of this class depends on the Pattern class:
    • Methods provided by Pattern:public Matcher matcher(CharSequence input);
    • After obtaining the Matcher class object, you can use the methods in this class to perform the following operations:
      • Regular matching:public boolean matches();
      • Regular replacement:public String replaceAll(String replacement);
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexStudy {
    
    

	public static void main(String[] args) throws Exception {
    
    
		String str = "101";		//要判断的数据
		String regex = "\\d+";		//正则表达式
		Pattern pat = Pattern.compile(regex);
		Matcher mat = pat.matcher(str);
		System.out.println(mat.matches());
	}
}

  If it is purely based on the three operations of split, replace, and match as an example, the java.utiol.regex development kit is not used at all, and it can be achieved only by relying on the String class. But there is a kind ofGroupingThe function of this grouping is not available in String.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexStudy {
    
    

	public static void main(String[] args) throws Exception {
    
    
		//要求取出“#{内容}”标记中的所有内容
		String str = "INSERT INTO dept(deptno,dname,loc) VALUES (#{deptno},#{dname},#{loc})";	//要判断的数据
		String regex = "#\\{\\w+\\}";		//正则表达式
		Pattern pat = Pattern.compile(regex);
		Matcher mat = pat.matcher(str);
		while(mat.find()) {
    
    		//是否有匹配成功的内容
			System.out.println(mat.group(0).replaceAll("#|\\{|\\}", ""));
		}
	}
}

Guess you like

Origin blog.csdn.net/MARVEL_3000/article/details/111567655