如何识别给定的字符串是十六进制颜色格式 [英] how to identify a given string is hex color format

查看:154
本文介绍了如何识别给定的字符串是十六进制颜色格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个正则表达式来验证 asp.net C#中的十六进制颜色。

并且还在服务器端查找验证代码。



例如: #CCCCCC

解决方案

  ^#(?:[0-9a-fA-F] {3}){1,2} $ 

解剖:

  ^锚起始字符串
#文本#
(组的开始
?:表示不捕获组,不生成反向引用
[0-9a-fA-F]十六进制数字
{3}
)结束组
{1,2}重复一次或两次
$ anchor结束字符串

这将匹配可以在CSS中使用的任意十六进制颜色值,例如#91bf4a code>#f13 。



注意:不支持RGBA十六进制颜色值。 / p>

I'm looking for a regular expression to validate hex colors in asp.net C#.
And also looking code for validation on server side.

For instance: #CCCCCC

解决方案

^#(?:[0-9a-fA-F]{3}){1,2}$

Dissection:

^              anchor for start of string
#              the literal #
(              start of group
 ?:            indicate a non-capturing group that doesn't generate backreferences
 [0-9a-fA-F]   hexadecimal digit
 {3}           three times
)              end of group
{1,2}          repeat either once or twice
$              anchor for end of string

This will match an arbitrary hexadecimal color value that can be used in CSS, such as #91bf4a or #f13.

Note: No support for RGBA hex color values, though.

这篇关于如何识别给定的字符串是十六进制颜色格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆