Use regular expressions in Java programming

  • 2020-04-01 04:09:19
  • OfStack

In the process of programming, it is often necessary to check the format of the input data. In this case, regular expressions are used. If the regular expressions are matched, the data format is correct, otherwise, the format is wrong. To check if the input matches a format, use the matches() method of the String class. The syntax is as follows:


boolean matches(String regex)

Regex: the specified regular expression.
Return value: returns a Boolean type.
This method is used to tell the current string whether it matches the regular expression specified by the parameter regex. The return value is of type Boolean and returns true if the current string matches the regular expression, or false if not.

A regular expression is composed of some contain special characters strings, these contain special character known as the yuan character, the following is a list of part of the regular expression metacharacters, in the regular expression method, to add "\" in front of the following metacharacters symbol for translation, such as yuan character "\ d" in the regular expression way is "\ d", but to ". "after the translation does not represent any one character, but said a specific period.

. : represents any character.
\d: any number from 0 to 9.
\D: for any non-numeric character.
\s: for white space character.
\S: for non - white space character.
\w: represents a character that can be used as an identifier, but does not include "$".
\W: represents a character that cannot be used as an identifier.
\p{Lower} : represents lowercase letters a ~ z.
\p{Upper} : represents Upper case letters A ~ A.
\p{ASCII} : ASCII character.
\p{Alpha} : alphabetic characters.
\p{Digit} : decimal Digit, 0 ~ 9.
\p{Alnum} : numeric or alphanumeric character.
\p{Punct} : punctuation.
\p{Graph} : visible characters.
\p{Print} : printable character.
\p{Blank} : Blank or TAB character.
\p{Cntrl} : control character.

When using regular expressions, if a metacharacter of a type is required to be output more than once, each input is rather cumbersome, then the qualifying character of the regular expression can be used to repeat the number of times. Common qualifiers and their meanings are listed below.

The & # 63; : 0 or 1 time.
* : 0 or more times.
+ : 0 or 1.
{n} : repeat n times.
{n,} : at least n times.
{n,m} : repeat n ~ m times.

In regular expressions, you can also enclose multiple characters in square brackets. The various regular expressions in square brackets represent different meanings. The metacharacters in square brackets and their meanings are listed below.

[ABC] : represents a, b or c.
[^ ABC] : represents any character other than a, b, and c.
[a-za-z] : any character of a ~ Z or a ~ Z.
[a-d[m-p]] : any character of a ~ d or m ~ p.
[a-z&&[def]] : d, e, or f.
[a-z&&[^ BC]] : all characters without b and c between a and z.
[a-z&&[^m-p]] : all characters without m-p between a-z.


Usage examples:
1. License plate number:




public static boolean validateCarNum(String carNum) {

boolean result = false;

String[] provence = new String[] { " Beijing ", " tianjin ", " ji ", " jin ", " liao ", " ji ", " black ", " Shanghai ", " Sue ", " zhejiang ", " anhui ", " fujian ", " jiangxi ", " lu ", " prepare ", " hubei ", " hunan ", " guangdong ", " guangxi ", " Joan ", " chongqing ",

" sichuan ", " qian ", " yunnan ", " hidden ", " shan ", " gump ", " green ", " ning ", " new ", " The port of ", " Australia ", " Mongolia " };

String reg = "[u4e00-u9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}";

boolean firstChar = false;

if (carNum.length() > 0) {

firstChar = Arrays.asList(provence).contains(carNum.substring(0, 1));

}

try {

Pattern p = Pattern.compile(reg);

Matcher m = p.matcher(carNum);

if (m.matches() && firstChar) {

result = true;

} else {

result = false;

}

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

2. Mobile phone number:




public static boolean isMobileNum(String mobileNum) {

boolean result = false;

try {

Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\D])|(18[0,5-9]))\d{8}$");

Matcher m = p.matcher(mobileNum);

result = m.matches();

} catch (Exception e) {

e.printStackTrace();

}

return result;

}


Mobile phone number + fixed telephone: 010-1111111,15516985859,0377-1111111


//Java detects whether it is a phone number (mobile phone, fixed phone verification)

String legalPhone = "";
String regExp ="^((13[0-9])|(15[^4,\D])|(18[0,5-9]))\d{8}|[0]{1}[0-9]{2,3}-[0-9]{7,8}$";
Pattern p = Pattern.compile(regExp);
Matcher m = p.matcher(importPotentialBFOs[i].getLegalPhone());
if(m.find()){ //Note: m.ind can only be used once and is false after the second call
 legalPhone = importPotentialBFOs[i].getLegalPhone();
 uploadTmp.setLegalTelephone(legalPhone);

}else{
 throw new BizException(" Wrong contact phone format !");
}

3. The real Numbers:


String[] arrs=new String[]{"a","1.123","-1.23","0","+111"};

      String regex="-?\d+\.?\d*";

      Pattern p = Pattern.compile(regex);

      for (int i = 0; i < arrs.length; i++) {

       Matcher m = p.matcher(arrs[i]);

       System.out.println(arrs[i]+":"+m.matches());

 }

Print:

A: false

1.123: true,

- 1.23: true

0: true,

+ 111: false


Related articles: