Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use RegexUtils

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use RegexUtils". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use RegexUtils.

Site

Regular correlation → RegexUtils.java → Test

IsMobileSimple: verify mobile phone number (simple) isMobileExact: verify mobile phone number (accurate) isTel: verify phone number isIDCard15: verify ID number 15 digits isIDCard18: verify ID number 18 digits isEmail: verify email isURL: verify URLisZh: verify Chinese characters isUsername: verify user name isDate Verify the date check in yyyy-MM-dd format Considered flat leap year isIP: verify IP address isMatch: determine whether to match regular getMatches: get regular matching part getSplits: get regular matching packet getReplaceFirst: replace the first part of regular match getReplaceAll: replace all regular matching parts

Specific route import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;import static com.blankj.utilcode.utils.ConstUtils.* / * * author: Blankj * blog: http://blankj.com * time: 2016-8-2 * desc: regular related tools * * / public class RegexUtils {private RegexUtils () {throw new UnsupportedOperationException ("u can't instantiate me...") } / * If u want more please visit http://toutiao.com/i6231678548520731137/ * / / * verify mobile phone number (simple) * * @ param input text to be verified * @ return {@ code true}: match

{@ code false}: does not match * / public static boolean isMobileSimple (CharSequence input) {return isMatch (REGEX_MOBILE_SIMPLE, input);} / * verify mobile phone number (accurate) * * @ param input text to be verified * @ return {@ code true}: match

{@ code false}: mismatch * / public static boolean isMobileExact (CharSequence input) {return isMatch (REGEX_MOBILE_EXACT, input);} / * * verify phone number * * @ param input text to be verified * @ return {@ code true}: match

{@ code false}: does not match * / public static boolean isTel (CharSequence input) {return isMatch (REGEX_TEL, input);} / * verify ID number 15 digits * * @ param input text to be verified * @ return {@ code true}: match

{@ code false}: does not match * / public static boolean isIDCard15 (CharSequence input) {return isMatch (REGEX_ID_CARD15, input);} / * verify ID number 18 digits * * @ param input text to be verified * @ return {@ code true}: match

{@ code false}: does not match * / public static boolean isIDCard18 (CharSequence input) {return isMatch (REGEX_ID_CARD18, input);} / * * verify mailbox * * @ param input text to be verified * @ return {@ code true}: match

{@ code false}: mismatch * / public static boolean isEmail (CharSequence input) {return isMatch (REGEX_EMAIL, input);} / * * verify URL * * @ param input text to be verified * @ return {@ code true}: match

{@ code false}: does not match * / public static boolean isURL (CharSequence input) {return isMatch (REGEX_URL, input);} / * * verifies Chinese characters * * @ param input text to be verified * @ return {@ code true}: match

{@ code false}: does not match * / public static boolean isZh (CharSequence input) {return isMatch (REGEX_ZH, input);} / * * verify user name *

The value range is "_". It cannot end with "_". The user name must be 6-20 digits * * @ param input text to be verified * @ return {@ code true}: match

{@ code false}: does not match * / public static boolean isUsername (CharSequence input) {return isMatch (REGEX_USERNAME, input);} / * verifies the date check in yyyy-MM-dd format, taking into account the flat leap year * * @ param input text to be verified * @ return {@ code true}: match

{@ code false}: mismatch * / public static boolean isDate (CharSequence input) {return isMatch (REGEX_DATE, input);} / * * verify IP address * * @ param input text to be verified * @ return {@ code true}: match

{@ code false}: do not match * / public static boolean isIP (CharSequence input) {return isMatch (REGEX_IP, input);} / * determine whether to match the regular * * @ param regex regular expression * @ param input string to match * @ return {@ code true}: match

{@ code false}: does not match * / public static boolean isMatch (String regex, CharSequence input) {return input! = null & & input.length () > 0 & & Pattern.matches (regex, input) } / * get part of regular match * * @ param regex regular expression * @ string to match * @ return part of regular match * / public static ListgetMatches (String regex, CharSequence input) {if (input = = null) return null; Listmatches = new ArrayList (); Pattern pattern = Pattern.compile (regex) Matcher matcher = pattern.matcher (input); while (matcher.find ()) {matches.add (matcher.group ());} return matches } / * get regular matching grouping * * @ param input string to be grouped * @ param regex regular expression * @ return regular matching grouping * / public static String [] getSplits (String input, String regex) {if (input = = null) return null; return input.split (regex) } / * replace the first part of regular match * * @ string to be replaced by param input * @ param regex regular expression * @ param replacement replacement * @ return replace the first part of regular match * / public static String getReplaceFirst (String input, String regex, String replacement) {if (input = = null) return null Return Pattern.compile (regex) .matcher (input) .replaceFirst (replacement) } / * replace all regular matching parts * * @ string to be replaced by param input * @ param regex regular expression * @ param replacement replacement * @ return replace all regular matching parts * / public static String getReplaceAll (String input, String regex, String replacement) {if (input = = null) return null Return Pattern.compile (regex) .matcher (input) .replaceAll (replacement);}} terminal

I believe that passengers have not seen the corresponding regular expressions, because I have done import static com.blankj.utilcode.utils.ConstUtils.*;, related constants are in this constant tool class, the relevant code is posted below.

/ * regular correlation constant * / / * regular: mobile phone number (simple) * / public static final String REGEX_MOBILE_SIMPLE = "^ [1]\\ d {10} $" / * regular: mobile phone number (accurate) * Mobile: 134 (0-8), 135, 136, 137, 138, 139, 147, 150, 151, 152, 157, 158, 159, 178, 182, 183, 184, 187, 188 * Unicom: 130, 131, 132, 145, 155, 156, 175, 176, 185, 186 * Telecom: 133, 153, 173, 177, 180, 181, 186 * Telecom 189 * Global Star: 1349 * Virtual operator: 170*/public static final String REGEX_MOBILE_EXACT = "^ ((13 [0-9]) | (14 [5jue 7]) | (15 [0-3 meme 5-9]) | (17 [0p3]) 5-8]) | (18 [0-9]) | (147)\\ d {8} $" / * regular: phone number * / public static final String REGEX_TEL = "^ 0\\ d {2helieu 3} [-]?\ d {7pr 8}" / * * regular: ID card number 15 digits * / public static final String REGEX_ID_CARD15 = "^ [1-9]\\ d {7} ((0\\ d) | (1 [0-2]) (([0 | 1 | 2]\\ d) | 3 [0-1])\\ d {3} $" / * * regular: ID card number 18 digits * / public static final String REGEX_ID_CARD18 = "^ [1-9]\\ d {5} [1-9]\\ d {3} ((0\\ d) | (1 [0-2]) (([0 | 1 | 2]\\ d) | 3 [0-1])\\ d {3} ([0-9Xx]) $" / * regular: mailbox * / public static final String REGEX_EMAIL = "^\\ w+ ([- +.]\\ w+) * @\\ w+ ([-.]\\ w+) *\.\ w+ ([-.]\\ w+) * $"; / * regular: URL * / public static final String REGEX_URL = "[a-zA-z] +: / / [^\\ s] *" / * regular: Chinese characters * / public static final String REGEX_ZH = "^ [\ u4e00 -\\ u9fa5] + $"; / * regular: user name, value range is: a ripzmae Aripz 0-9, "_", Chinese characters, cannot end with "_", the user name must be 6-20 digits * / public static final String REGEX_USERNAME = "^ [\ w\ u4e00 -\ u9fa5] {6CoE 20} (? At this point, I believe you have a deeper understanding of "how to use RegexUtils". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report