¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, DreamLu All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: DreamLu 墿¥æ¢¦ (596392912@qq.com) |
| | | */ |
| | | package org.springblade.core.tool.utils; |
| | | |
| | | import org.springframework.lang.Nullable; |
| | | |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | /** |
| | | * æ£å表达å¼å·¥å
· |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class RegexUtil { |
| | | /** |
| | | * ç¨æ·å |
| | | */ |
| | | public static final String USER_NAME = "^[a-zA-Z\\u4E00-\\u9FA5][a-zA-Z0-9_\\u4E00-\\u9FA5]{1,11}$"; |
| | | |
| | | /** |
| | | * å¯ç |
| | | */ |
| | | public static final String USER_PASSWORD = "^.{6,32}$"; |
| | | |
| | | /** |
| | | * é®ç®± |
| | | */ |
| | | public static final String EMAIL = "^\\w+([-+.]*\\w+)*@([\\da-z](-[\\da-z])?)+(\\.{1,2}[a-z]+)+$"; |
| | | |
| | | /** |
| | | * ææºå· |
| | | */ |
| | | public static final String PHONE = "^1[3456789]\\d{9}$"; |
| | | |
| | | /** |
| | | * ææºå·æè
é®ç®± |
| | | */ |
| | | public static final String EMAIL_OR_PHONE = EMAIL + "|" + PHONE; |
| | | |
| | | /** |
| | | * URLè·¯å¾ |
| | | */ |
| | | public static final String URL = "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})(:[\\d]+)?([\\/\\w\\.-]*)*\\/?$"; |
| | | |
| | | /** |
| | | * èº«ä»½è¯æ ¡éªï¼åçº§æ ¡éªï¼å
·ä½è§åæä¸å¥ç®æ³ |
| | | */ |
| | | public static final String ID_CARD = "^\\d{15}$|^\\d{17}([0-9]|X)$"; |
| | | |
| | | /** |
| | | * ååæ ¡éª |
| | | */ |
| | | public static final String DOMAIN = "^[0-9a-zA-Z]+[0-9a-zA-Z\\.-]*\\.[a-zA-Z]{2,4}$"; |
| | | |
| | | /** |
| | | * ç¼è¯ä¼ å
¥æ£å表达å¼åå符串å»å¹é
,忽ç¥å¤§å°å |
| | | * |
| | | * @param regex æ£å |
| | | * @param beTestString å符串 |
| | | * @return {boolean} |
| | | */ |
| | | public static boolean match(String regex, String beTestString) { |
| | | Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); |
| | | Matcher matcher = pattern.matcher(beTestString); |
| | | return matcher.matches(); |
| | | } |
| | | |
| | | /** |
| | | * ç¼è¯ä¼ å
¥æ£å表达å¼å¨å符串ä¸å¯»æ¾ï¼å¦æå¹é
å°å为true |
| | | * |
| | | * @param regex æ£å |
| | | * @param beTestString å符串 |
| | | * @return {boolean} |
| | | */ |
| | | public static boolean find(String regex, String beTestString) { |
| | | Pattern pattern = Pattern.compile(regex); |
| | | Matcher matcher = pattern.matcher(beTestString); |
| | | return matcher.find(); |
| | | } |
| | | |
| | | /** |
| | | * ç¼è¯ä¼ å
¥æ£å表达å¼å¨å符串ä¸å¯»æ¾ï¼å¦ææ¾å°è¿å第ä¸ä¸ªç»æ |
| | | * æ¾ä¸å°è¿ånull |
| | | * |
| | | * @param regex æ£å |
| | | * @param beFoundString å符串 |
| | | * @return {boolean} |
| | | */ |
| | | @Nullable |
| | | public static String findResult(String regex, String beFoundString) { |
| | | Pattern pattern = Pattern.compile(regex); |
| | | Matcher matcher = pattern.matcher(beFoundString); |
| | | if (matcher.find()) { |
| | | return matcher.group(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | } |