Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 仅当数字不是十六进制颜色代码的一部分时才匹配该数字_Php_Regex_Colors - Fatal编程技术网

Php 仅当数字不是十六进制颜色代码的一部分时才匹配该数字

Php 仅当数字不是十六进制颜色代码的一部分时才匹配该数字,php,regex,colors,Php,Regex,Colors,我正在尝试匹配数字并将其替换为(匹配数字)px 问题是,我只想在它们不是十六进制颜色代码的一部分时匹配它们。我的输入可以通过两种方式包含十六进制颜色代码: #xxx或#xxxxxx其中x可以是a-f中的字母或0-9中的数字 我目前拥有的正则表达式如下: $input=preg_replace('/(?px在它们之后。谢谢 编辑:由于反向查找不能包含无限数量的字符(即没有量词),我不知道该怎么做 输入和输出应如下所示: 输入:#da4 10 输出:#da4 10px 输入:#122222 10 输

我正在尝试匹配数字并将其替换为
(匹配数字)px

问题是,我只想在它们不是十六进制颜色代码的一部分时匹配它们。我的输入可以通过两种方式包含十六进制颜色代码:
#xxx
#xxxxxx
其中
x
可以是
a-f
中的字母或
0-9
中的数字

我目前拥有的正则表达式如下:

$input=preg_replace('/(?px
在它们之后。谢谢

编辑:由于反向查找不能包含无限数量的字符(即没有量词),我不知道该怎么做

输入和输出应如下所示:

输入:#da4 10 输出:#da4 10px

输入:#122222 10 输出:#122222 10px

输入:#4444dd 20px 输出:#4444dd 20px

输入3010200#4142099#da4 输出:30px 10px 20px 20px#414 20px 99px#da4

您可以使用
(?(替换为
$1px
)。


说明:

(?<!#)   make sure this isn't hex
\b       make sure we're matching the whole number, not just a part of it
(\d+)    capture the number
\b       again, make sure we've captured the whole number
(?!px)   make sure there's no px
(?您可以使用
(?(替换为
$1px
)。


说明:

(?<!#)   make sure this isn't hex
\b       make sure we're matching the whole number, not just a part of it
(\d+)    capture the number
\b       again, make sure we've captured the whole number
(?!px)   make sure there's no px
(?正则表达式:

\b(?<!#)\d+\b
# \b     Assert position at a word boundary 
# (?<!#) Negative Lookbehind
# \d+    Match a number with 1 to ilimited digits 
# \b     Assert position at a word boundary 

$input = '30 10 20 20 #414 20 99 #da4 #122222 10 #4444dd 20px';
$input = preg_replace('/\b(?<!#)\d+\b/', '$0px', $input);
print($input);
\b(?)?
正则表达式:

\b(?<!#)\d+\b
# \b     Assert position at a word boundary 
# (?<!#) Negative Lookbehind
# \d+    Match a number with 1 to ilimited digits 
# \b     Assert position at a word boundary 

$input = '30 10 20 20 #414 20 99 #da4 #122222 10 #4444dd 20px';
$input = preg_replace('/\b(?<!#)\d+\b/', '$0px', $input);
print($input);
\b(?)?

你能添加一个样本列表,列出哪些应该匹配,哪些不应该匹配吗?从
(\d|[a-f])
开始进行十六进制检查。
3
,或者
6
字符长度会有点困难。我稍微更改了你的正则表达式-@JorgeCampos,我添加了这个信息:)
\b(?你能添加一个样本列表,列出哪些应该匹配,哪些不应该匹配吗?请从
(\d|[a-f])
开始进行十六进制检查。
3
,或者
6
字符长会有点困难。我稍微更改了你的正则表达式-@JorgeCampos我添加了这个信息:)
\b(?