如何使用负向回顾
基本上,我将给定样式表中具有蓝色色调的任何和所有十六进制值更改为其红色色调对应项(即 #00f
更改为 #ff0000
(我的函数输出六个字符的十六进制值(不包括 #
))。
创建正则表达式来匹配十六进制颜色不是问题(我不关心 HTML 颜色名称,尽管我最终可能关心 rgb
、rgba< /code>、
hsb
等值。)。这就是我最终得到的结果 #(([0-9A-z]{3}){1,2})
。它有效,但我希望它是完整的证据。例如,如果有人碰巧使用带有有效十六进制值的片段(即#top
)设置背景图像,我不想更改它。我尝试做消极的回顾,但它似乎不起作用。我使用 \B#(([0-9A-z]{3}){1,2})
但如果存在单词边界(例如空格) ) 在“#”之前,它匹配 URL 片段。这就是我认为应该解决的问题,但没有: (?。
我正在使用桌面版本的 RegExr 来测试以下样式表:
body {
background: #f09 url('images#06F');
}
span {
background=#00f url('images#889');
}
div {
background:#E4aaa0 url('images#889');
}
h1 {
background: #fff #dddddd;
}
每当我将鼠标悬停在在 (? 子字符串上,RegExr 将其识别为“与 '
url\([^#)]*
匹配的“负向查找ahead” ’。”有没有可能一个错误或者我只是有一个糟糕的正则表达式日?当我们这样做时,是否有任何其他上下文将“#”用于非十六进制目的?
编辑:好吧,我不能提前编程 ?那个十六进制正则表达式应该是 #(([0-9A-Fa-f]{3}){1,2})
编辑 2:好吧,所以我错过了大多数语言的细节需要静态长度后视。
Basically, I am changing any and all hexadecimal values with a blue hue to its red hue counterpart in a given stylesheet (i.e. #00f
is changed to #ff0000
(my function outputs six character hexadecimal values excluding the #
)).
It was not a problem creating a regular expression to match hexadecimal colors (I'm not concerned about HTML color names although I may eventually care about rgb
, rgba
, hsb
, etc. values.). This is what I ended up with #(([0-9A-z]{3}){1,2})
. It works but I want it to be full proof. For example, if somebody happens to set a background image with a fragment (i.e. #top
) with a valid hexadecimal value, I don't want to change it. I tried doing a negative lookbehind, but it doesn't seem to work. I was using \B#(([0-9A-z]{3}){1,2})
but if there is a word boundary (such as a space) before the '#', it match the URL fragment. This is what I thought should do the trick but doesn't: (?<!url\([^#)]*)#(([0-9A-z]{3}){1,2})
.
I am using the desktop version of RegExr to test with the following stylesheet:
body {
background: #f09 url('images#06F');
}
span {
background=#00f url('images#889');
}
div {
background:#E4aaa0 url('images#889');
}
h1 {
background: #fff #dddddd;
}
Whenever, I hover over the (?<!
substring, RegExr identifies it as a "Negative lookahead matching 'url\([^#)]*
'." Could there be a bug or am I just having a bad regex day? And while we're at it, are there any other contextes in which a '#' is used for non-hexadecimal purposes?
EDIT: Alright, I can't program early in the morning. That hexadecimal regex should be #(([0-9A-Fa-f]{3}){1,2})
EDIT 2: Alright, so I missed the detail that most languages require static length lookbehinds.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要的是以下解决方案之一或其他
结果
编辑 1
结果
编辑 2
结果
正则表达式非常强大,条件是必须有足够的字符串部分遵循某个组织,并且在可变的其他部分之间具有相对稳定性是为了被抓住。如果分析的文本结构过于松散,则无法编写正则表达式。
您的琴弦是否还有许多其他“类似丑角的拼凑”结构?
I think that what you need is either one of the following solutions or the other
result
Edit 1
result
Edit 2
result
Regexes are extremely powerful in the condition that there must be enough portions of strings following a certain organisation having relative stability among variable other portions that are intended to be catched. If the analyzed text becomes too loose in its structure, it becomes impossible to write a regex.
Are there still a lot of other "Harlequin-like patchwork" structures possible for your strings ??