如何使用正则表达式验证十六进制颜色代码

十六进制颜色代码正则表达式模式

^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$

描述

^		 #start of the line
 #		 #  must constains a "#" symbols
 (		 #  start of group #1
  [A-Fa-f0-9]{6} #    any strings in the list, with length of 6
  |		 #    ..or
  [A-Fa-f0-9]{3} #    any strings in the list, with length of 3
 )		 #  end of group #1 
$		 #end of the line

整个组合是指字符串必须以“#”符号开头,后跟一个从“ a”到“ f”,“ A”到“ Z”的字母或从“ 0”到9”的数字(正好是6或3)长度。 此正则表达式模式对于十六进制Web颜色代码检查非常有用。

Java正则表达式示例

package com.mkyong.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class HexValidator{
	
   private Pattern pattern;
   private Matcher matcher;
 
   private static final String HEX_PATTERN = "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$";
	        
   public HexValidator(){
	  pattern = Pattern.compile(HEX_PATTERN);
   }
	  
   /**
   * Validate hex with regular expression
   * @param hex hex for validation
   * @return true valid hex, false invalid hex
   */
   public boolean validate(final String hex){
		  
	  matcher = pattern.matcher(hex);
	  return matcher.matches();
	    	    
   }
}

匹配的十六进制代码:

1.“#1f1f1F”,“#AFAFAF”,“#1AFFa1”,“#222fff”,“#F00”,“#F00”

不匹配的十六进制代码:

1.“ 123456” –必须以“#”符号开头
2.“ #afafah” –“ h”是不允许的,从“ a”到“ f”的有效字母
3.“#123abce” – 6个长度或3个长度
4.“ aFaE3f” –必须以“#”符号开头,长度为6或3
5.“ F00” –必须以“#”符号开头
6.“ #afaf” – 6个长度或3个长度
7.“#F0h” –“ h”是不允许的,从“ a”到“ f”的有效字母

单元测试– HexValidator

package com.mkyong.regex;

import org.testng.Assert;
import org.testng.annotations.*;
 
/**
 * Hex validator Testing
 * @author mkyong
 *
 */
public class HexValidatorTest {
 
	private HexValidator hexValidator;
    
	@BeforeClass
        public void initData(){
		hexValidator = new HexValidator();
        }
    
	@DataProvider
	public Object[][] ValidHexProvider() {
		return new Object[][]{
			   {new String[] {
				"#1f1f1F", "#AFAFAF","#1AFFa1","#222fff", "#F00"
			   }}
		};
	}
	
	@DataProvider
	public Object[][] InvalidHexProvider() {
		return new Object[][]{
			{new String[] {
				   "123456","#afafah","#123abce","aFaE3f",
				   "F00","#afaf", "#F0h"	 
		        }}
		};
	}
	
	@Test(dataProvider = "ValidHexProvider")
	public void ValidHexTest(String[] hex) {
		
	   for(String temp : hex){
		   boolean valid = hexValidator.validate(temp);
		   System.out.println("Hex is valid : " + temp + " , " + valid);
		   Assert.assertEquals(true, valid);
	   }
	   
	}
	
	@Test(dataProvider = "InvalidHexProvider", dependsOnMethods="ValidHexTest")
	public void InValidHexTest(String[] hex) {
		
	   for(String temp : hex){
		   boolean valid = hexValidator.validate(temp);
		   System.out.println("Hex is valid : " + temp + " , " + valid);
		   Assert.assertEquals(false, valid);
	   }
	   
	}
}

单元测试–结果

Hex is valid : #1f1f1F , true
Hex is valid : #AFAFAF , true
Hex is valid : #1AFFa1 , true
Hex is valid : #222fff , true
Hex is valid : #F00 , true
Hex is valid : 123456 , false
Hex is valid : #afafah , false
Hex is valid : #123abce , false
Hex is valid : aFaE3f , false
Hex is valid : F00 , false
Hex is valid : #afaf , false
Hex is valid : #F0h , false
PASSED: ValidHexTest([Ljava.lang.String;@1a626f)
PASSED: InValidHexTest([Ljava.lang.String;@e5855a)

===============================================
    com.mkyong.regex.HexValidatorTest
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
mkyong
Total tests run: 2, Failures: 0, Skips: 0
===============================================

参考

http://en.wikipedia.org/wiki/Web_colors

想更多地了解正则表达式? 强烈推荐这本最好的经典书籍-“掌握正则表达式”



翻译自: https://mkyong.com/regular-expressions/how-to-validate-hex-color-code-with-regular-expression/

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值