2 * Copyright 2016 2015-2016 ZTE, Inc. and others. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.openo.msb.wrapper.util;
18 import java.util.regex.Pattern;
20 public class RegExpTestUtil {
24 public static boolean hostRegExpTest(String host){
26 String hostReg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
27 +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
28 +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
29 +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)"
31 return Pattern.matches(hostReg, host);
35 public static boolean ipRegExpTest(String ip){
37 String hostReg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
38 +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
39 +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
40 +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
41 return Pattern.matches(hostReg, ip);
45 public static boolean portRegExpTest(String port){
47 String hostReg = "^\\d{1,5}$";
48 return Pattern.matches(hostReg, port);
52 public static boolean versionRegExpTest(String version){
54 String versionReg = "^v\\d+(\\.\\d+)?$";
55 return Pattern.matches(versionReg, version);
59 public static boolean urlRegExpTest(String url){
60 if(url.equals("/")) return true;
62 String urlReg = "^\\/.*((?!\\/).)$";
63 return Pattern.matches(urlReg, url);
67 public static boolean apiRouteUrlRegExpTest(String url){
69 String urlReg = "^\\/"+RouteUtil.API_ROOT_PATH+"\\/.*$";
70 return Pattern.matches(urlReg, url);
74 public static boolean iuiRouteUrlRegExpTest(String url){
76 String urlReg = "^\\/"+RouteUtil.IUI_ROOT_PATH+"\\/.*$";
77 return Pattern.matches(urlReg, url);
84 public static void main(String[] args) {
85 System.out.println(urlRegExpTest("/api "));
86 // System.out.println("api".startsWith("/"));