Good Regex Detecting for Website URL, ISBN, GUID - Java
// web site url
public static boolean WebsiteURL(final String str)
{
if(str == null) return false;
String regex = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
Pattern pattern = Pattern.compile( regex );
return pattern.matcher( str.toString() ).matches();
}
// ISBN 國際標準書號
public static boolean LibraryISBN(final String str)
{
if(str == null) return false;
String regex = "^(/d[- ]*){9}[/dxX]$";
Pattern pattern = Pattern.compile( regex );
return pattern.matcher( str.toString() ).matches();
}
// GUID 全球唯一標識符
public static boolean GUID(final String str)
{
if(str == null) return false;
String regex = "^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$";
Pattern pattern = Pattern.compile( regex );
return pattern.matcher( str.toString() ).matches();
}
public static boolean WebsiteURL(final String str)
{
if(str == null) return false;
String regex = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
Pattern pattern = Pattern.compile( regex );
return pattern.matcher( str.toString() ).matches();
}
// ISBN 國際標準書號
public static boolean LibraryISBN(final String str)
{
if(str == null) return false;
String regex = "^(/d[- ]*){9}[/dxX]$";
Pattern pattern = Pattern.compile( regex );
return pattern.matcher( str.toString() ).matches();
}
// GUID 全球唯一標識符
public static boolean GUID(final String str)
{
if(str == null) return false;
String regex = "^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$";
Pattern pattern = Pattern.compile( regex );
return pattern.matcher( str.toString() ).matches();
}
留言
張貼留言