00 09Java of advanced regular expressions

1 understanding regular expressions

Can be found through a series of analysis before, String is a very versatile type as String processing operations not only supports a variety of string, also supports the conversion to each data type function, so in the development of the project, as long as the user information entered by basically expressed String. So when converted to other data types, in order to ensure the accuracy of conversion, often have to be some complex validation process, then this case, if simply rely on the method of the String class is very troublesome.

Now assume that you have a character string request determines whether the string of figures, the digit number if the number and then it becomes multiplication.
example:

package cn.victor.demo;

import java.text.NumberFormat;
import java.text.ParseException;

public class DateDemo {

	public static void main(String[] args) throws ParseException {
		String str = "123";
		if(isNumber(str)) {
			long n = Integer.parseInt(str);
			System.out.println(n * 2);
		}
		
	}
	
	public static boolean isNumber(String str) {
		char[] chars = str.toCharArray();
		for(int i = 0; i < chars.length; i++) {
			if( chars[i] < '0' || chars[i] > '9' ) {
				return false;
			}
		}
		
		return true;
	}
}


This verification function is actually very simple, but this is such a simple function it takes a lot of developers to write application logic code, then if it is a more complex validation? So in this case, to verify terms of best practice is to use regular expressions to complete.
Example: use regular expressions to achieve the same effect

package cn.victor.demo;

import java.text.NumberFormat;
import java.text.ParseException;

public class DateDemo {

	public static void main(String[] args) throws ParseException {
		String str = "123";
		if(str.matches("\\d+")) {
			long n = Integer.parseInt(str);
			System.out.println(n * 2);
		}
		
	}
}


Regular expressions originally from Perl language which evolved, then in JDK 1.4 before if you need to use the regular expression definitions, it is necessary to introduce other jar file alone, but from the after JDK 1.4, regular tacitly been the JDK support, and is provided with java.util.regex development package, while specific to the String class also made some changes, so there are ways to directly support the regular process.

Using regular biggest feature is to facilitate the verification process, modification process and to facilitate complex string.

Common numerals 2 Regular

If it is to carry out regular processing operations, then it first needs to commonly used regular markers have mastered, from the beginning of JDK 1.4 provides java.util.regex development package, this package which provides a Pattern classes, classes in this program which have all supported the definition of regular mark.
(1) [Number: single character match]
| - any character: represented by any characters
| - \\: Match "\";
| - \n: Match newline;
| - \t: matching tabs;
(2) [ number:] single character set (which may be optionally from a character)
| - : [abc]indicates the letter may be a, b, c is an arbitrary character;
| - : [\^abc]not a represents any letter a, b, c is ;
| - [a-zA-Z]: is represented by a letter composed in any case-insensitive;
| - [0-9]: represented by a numbers.
(3) [Number: single simplified character set]
| - .: represents any character;
| - \d: equivalent to the range [0-9];
| - \D: equivalent to the range [^0-9];
| - \s: match any of a space, It may be a space, line feed, tabs;
| - \S: match any non-space data;
| - \w: matching letters, numbers, underscores, equivalent to [a-zA-Z_0-9];
| - \W: Non-match letters, numbers, underscores, equivalent to [^a-zA-Z_0-9];
(4) boundary matching
| - ^: Matching border began;
| - $: match ended boundary;
(5) the number indicated by default only add on the number of units before they can match the number of characters.
? | - expression: the regularization can occur 0 or 1 times.
| - * expression: the positive may occur zero, one or more times;
| - + expression: the positive may occur one or more times;
| -} {n-expression: length of the expression exactly n times;
:; | - expression {n,} length expression is at least n times
; length of the expression of times n ~ m: | - expression {n, m}
logical expression (6) type: you can connect multiple regular:
| - expression expression Y X: X expression followed after the Y expression.
| - expression X | expression Y: There can be an expression meet.
| - (expression): a whole is described as an expression settings, you can set the number of units for the entire description.

3 String class support for canonical

Performing regular expression process will in most cases be done based on the String Class, and is provided with the following operating method in which the String class:

No. Method name Types of description
01 public boolean matches​(String regex) ordinary Specified regular string determination
02 public String replaceAll​(String regex, String replacement) ordinary Replace All
03 public String replace​(char oldChar, char newChar) ordinary Replace first
04 public String[] split​(String regex) ordinary Regular Split
05 public String[] split​(String regex, int limit) ordinary Regular Split

By following some specific examples to get on the regular use will be explained.
Example: an alternative implementation (delete non-alphanumeric) string

package cn.victor.demo;

public class DateDemo {

	public static void main(String[] args) {
		String str = "328d$%$(fdf32(*&c34f34c24334gfg34c324c&crfgdsfg^%^$324csdsd##";
		String replstr = str.replaceAll("[^a-zA-Z0-9]", "");
		System.out.println(replstr);
		
	}
}


Example: a string for split (split by number)

package cn.victor.demo;

public class DateDemo {

	public static void main(String[] args) {
		String str = "328d$%$(fdf32(*&c34f34c24334gfg34c324c&crfgdsfg^%^$324csdsd##";
		String replstr[] = str.split("\\d+");
		for(int i = 0; i < replstr.length; i++) {
			System.out.println(replstr[i]);
		}
		
	}
}


When a regular process for the operation of the replacement of the split relatively easy, but the problem was the data verification section.
Example: determining whether the data is a decimal, the decimal is then if it becomes a double

package cn.victor.demo;

public class DateDemo {

	public static void main(String[] args) {
		String str = "23";
		System.out.println(str.matches("\\d+(\\.\\d+)?"));
	}
}


Example: determining whether a string consisting of the date, if the date is composed of type Date then it is converted to

package cn.victor.demo;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class DateDemo {

	public static void main(String[] args) throws ParseException {
		String str = "1999-08-21";
		if(str.matches("\\d{4}-\\d{2}-\\d{2}"))
		{
			System.out.println(new SimpleDateFormat("yyyy-MM-dd").parse(str));
		}
	}
}


Regular specific meaning can not be determined, only determination processing for format.
Example: determine whether a given phone number correct?
(1) phone numbers: \\d{7,8}51283346, ;
(2) the telephone number: (\\d{3,4})?\\d{7,8}01051283346, ;
(3) telephone number: (010) ((\\d{3,4})|(\\(\\d{3,4}\\))-)?\\d{7,8}-51,283,346, .

package cn.victor.demo;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class DateDemo {

	public static void main(String[] args) throws ParseException {
		String str = "(010)-5128331";
		System.out.println(str.matches("((\\d{3,4})|(\\(\\d{3,4}\\)))?-?\\d{7,8}"));
	}
}


Now that can be used to verify a regular, then the following can use it to implement a verification email address format.
Example: Verify email format
(. 1) may be formed email username 字母, 数字, _composed;
(2) may be formed email domain 字母, 数字, _, -composed;
suffix (3) the domain name must .cnbe: .com, .net, .com.cn, .gov, ;

package cn.victor.demo;

public class DateDemo {

	public static void main(String[] args) {
		String str = "[email protected]";
		System.out.println(str.matches("[^_]\\w+@[\\w-]+\\.(com|cn|com.cn|edu|gov)"));
	}
}


Now that several regular matching processing operation is the most common form of treatment are several.

4 java.util.regex support package

Although in most cases you can use the String class to achieve regular operation, but there are some cases need to use regular processing class package provided java.util.regex development in this package which has defined two classes: Pattren (regular expression compiler), Matcher (match).
1, Pattern Class
(1) Pattern class provides support for the compile processing regular expressions: public static Pattern compile​(String regex);
(2) is also provided with a support string split operation:public String[] split​(CharSequence input)

package cn.victor.demo;

import java.util.regex.Pattern;

public class DateDemo {

	public static void main(String[] args) {
		String str = "1s2s3s4";
		Pattern pat = Pattern.compile("s");
		String[] resultset = pat.split(str);
		for(int i = 0; i < resultset.length; i++) {
			System.out.println(resultset[i]);
		}
	}
}


2, Matcher class that implements the processing Regularization matching instances of objects of this class relies on Pattern class completed.
(1) Method Pattern class provides: public Matcher matcher​(CharSequence input)
When the acquired object class can Matcher proceeds as follows using the method in this class:
(1) regular Match:public boolean matches()

package cn.victor.demo;

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

public class DateDemo {

	public static void main(String[] args) {
		String str = "123";
		Pattern pat = Pattern.compile("\\d+");
		Matcher mac = pat.matcher(str);
		System.out.println(mac.matches());
	}
}


(2) replacement string:public String replaceAll​(String replacement)

package cn.victor.demo;

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

public class DateDemo {

	public static void main(String[] args) {
		String str = "1s2s3";
		Pattern pat = Pattern.compile("s");
		Matcher mac = pat.matcher(str);
		System.out.println(mac.replaceAll(""));
	}
}


If it is purely based on the split, replace, match three operations, for example, never use java.util.regex development kit, relying only on the String class can be achieved. However, there is provided a Matcher class grouping function, and function of this packet is not available in String.

package cn.victor.demo;

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

public class DateDemo {

	public static void main(String[] args) {
		String str = "select * from table values(#{dept},#{no},#{name})";
		Pattern pat = Pattern.compile("#\\{\\w+\\}");
		Matcher mac = pat.matcher(str);
		while(mac.find()) {
			System.out.println(mac.group(0).replaceAll("\\W+", ""));
		}
	}
}


java.util.regex development kit, if not carried out some of the more complex regular treatment is difficult to use, and functionality provided by the String class is only suitable for the basic operation of regular.

Published 87 original articles · won praise 11 · views 2989

Guess you like

Origin blog.csdn.net/weixin_43762330/article/details/104770176