如何使用负向回顾

发布于 2024-11-29 16:46:32 字数 1190 浏览 2 评论 0原文

基本上,我将给定样式表中具有蓝色色调的任何和所有十六进制值更改为其红色色调对应项(即 #00f 更改为 #ff0000 (我的函数输出六个字符的十六进制值(不包括 #))。

创建正则表达式来匹配十六进制颜色不是问题(我不关心 HTML 颜色名称,尽管我最终可能关心 rgbrgba< /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

和影子一齐双人舞 2024-12-06 16:46:32

我认为您需要的是以下解决方案之一或其他

ss = '''    background: #f09 url('images#06F'); 
    background=#00f url('images #889'); 
    background:#E4aaa0 url('images#890'); 
    background: #fff #dddddd; '''

print ss
import re

three = '(?:[0-9A-Fa-f]{3})'

regx = re.compile('^ *background[ =:]*#(%s{1,2})' % three,re.MULTILINE)
print regx.findall(ss)

print '-----------------------------------------------------'

regx = re.compile('(?:(?:^ *background[ =:]*)|(?:(?<=#%s)|(?<=#%s%s)) +)'
                  '#(%s{1,2})' % (three,three,three,three),
                  re.MULTILINE)
print regx.findall(ss)

结果

    background: #f09 url('images#06F'); 
    background=#00f url('images #889'); 
    background:#E4aaa0 url('images#890'); 
    background: #fff #dddddd; 
['f09', '00f', 'E4aaa0', 'fff']
-----------------------------------------------------
['f09', '00f', 'E4aaa0', 'fff', 'dddddd']

编辑 1

ss = '''    background: #f09 url('images#06F'); 
    background=#00f url('images #889'); 
    color:#E4aaa0 url('images#890'); 
    background: #fff #dddddd#125e88    #ae3;
    Walter (Elias) Disney: #f51f51 '''

print ss+'\n'

import re

three = '(?:[0-9A-Fa-f]{3})'

regx = re.compile('^ *[^=:]+[ =:]*#(%s{1,2})' % three,re.MULTILINE)
print regx.findall(ss)

print '-----------------------------------------------------'

regx = re.compile('(?:(?:^ *[^=:]+[ =:]*)|(?:(?<=#%s)|(?<=#%s%s)) *)'
                  '#(%s{1,2})' % (three,three,three,three),
                  re.MULTILINE)
print regx.findall(ss)

结果

    background: #f09 url('images#06F'); 
    background=#00f url('images #889'); 
    color:#E4aaa0 url('images#890'); 
    background: #fff #dddddd#125e88    #ae3;
    Walter (Elias) Disney: #f51f51 

['f09', '00f', 'E4aaa0', 'fff', 'f51f51']
-----------------------------------------------------
['f09', '00f', 'E4aaa0', 'fff', 'dddddd', '125e88', 'ae3', 'f51f51']

编辑 2

ss = '''    background: #f09 url('images#06F'); 
    background=#00f url('images #889'); 
    color:#E4aaa0 url('images#890'); 
    background: #fff #dddddd#125e88    #ae3;
    Walter (Elias) Disney: #f51f51
    background: -webkit-gradient(linear, from(#000000), to(#ffffff));. '''

print ss+'\n'

import re

three = '(?:[0-9A-Fa-f]{3})'

preceding = ('(?:(?:^[^#]*)'
                 '|'
                 '(?:(?<=#%s)'
                     '|'
                     '(?<=#%s%s)'
                     '|'
                     '(?<= to\()'
                     ')'
                 ')') % (three,three,three)

regx = re.compile('%s *#(%s{1,2})' % (preceding,three), re.MULTILINE)
print regx.findall(ss)

结果

    background: #f09 url('images#06F'); 
    background=#00f url('images #889'); 
    color:#E4aaa0 url('images#890'); 
    background: #fff #dddddd#125e88    #ae3;
    Walter (Elias) Disney: #f51f51
    background: -webkit-gradient(linear, from(#000000), to(#ffffff));. 

['f09', '00f', 'E4aaa0', 'fff', 'dddddd', '125e88', 'ae3', 'f51f51', '000000', 'ffffff']

正则表达式非常强大,条件是必须有足够的字符串部分遵循某个组织,并且在可变的其他部分之间具有相对稳定性是为了被抓住。如果分析的文本结构过于松散,则无法编写正则表达式。

您的琴弦是否还有许多其他“类似丑角的拼凑”结构?

I think that what you need is either one of the following solutions or the other

ss = '''    background: #f09 url('images#06F'); 
    background=#00f url('images #889'); 
    background:#E4aaa0 url('images#890'); 
    background: #fff #dddddd; '''

print ss
import re

three = '(?:[0-9A-Fa-f]{3})'

regx = re.compile('^ *background[ =:]*#(%s{1,2})' % three,re.MULTILINE)
print regx.findall(ss)

print '-----------------------------------------------------'

regx = re.compile('(?:(?:^ *background[ =:]*)|(?:(?<=#%s)|(?<=#%s%s)) +)'
                  '#(%s{1,2})' % (three,three,three,three),
                  re.MULTILINE)
print regx.findall(ss)

result

    background: #f09 url('images#06F'); 
    background=#00f url('images #889'); 
    background:#E4aaa0 url('images#890'); 
    background: #fff #dddddd; 
['f09', '00f', 'E4aaa0', 'fff']
-----------------------------------------------------
['f09', '00f', 'E4aaa0', 'fff', 'dddddd']

Edit 1

ss = '''    background: #f09 url('images#06F'); 
    background=#00f url('images #889'); 
    color:#E4aaa0 url('images#890'); 
    background: #fff #dddddd#125e88    #ae3;
    Walter (Elias) Disney: #f51f51 '''

print ss+'\n'

import re

three = '(?:[0-9A-Fa-f]{3})'

regx = re.compile('^ *[^=:]+[ =:]*#(%s{1,2})' % three,re.MULTILINE)
print regx.findall(ss)

print '-----------------------------------------------------'

regx = re.compile('(?:(?:^ *[^=:]+[ =:]*)|(?:(?<=#%s)|(?<=#%s%s)) *)'
                  '#(%s{1,2})' % (three,three,three,three),
                  re.MULTILINE)
print regx.findall(ss)

result

    background: #f09 url('images#06F'); 
    background=#00f url('images #889'); 
    color:#E4aaa0 url('images#890'); 
    background: #fff #dddddd#125e88    #ae3;
    Walter (Elias) Disney: #f51f51 

['f09', '00f', 'E4aaa0', 'fff', 'f51f51']
-----------------------------------------------------
['f09', '00f', 'E4aaa0', 'fff', 'dddddd', '125e88', 'ae3', 'f51f51']

Edit 2

ss = '''    background: #f09 url('images#06F'); 
    background=#00f url('images #889'); 
    color:#E4aaa0 url('images#890'); 
    background: #fff #dddddd#125e88    #ae3;
    Walter (Elias) Disney: #f51f51
    background: -webkit-gradient(linear, from(#000000), to(#ffffff));. '''

print ss+'\n'

import re

three = '(?:[0-9A-Fa-f]{3})'

preceding = ('(?:(?:^[^#]*)'
                 '|'
                 '(?:(?<=#%s)'
                     '|'
                     '(?<=#%s%s)'
                     '|'
                     '(?<= to\()'
                     ')'
                 ')') % (three,three,three)

regx = re.compile('%s *#(%s{1,2})' % (preceding,three), re.MULTILINE)
print regx.findall(ss)

result

    background: #f09 url('images#06F'); 
    background=#00f url('images #889'); 
    color:#E4aaa0 url('images#890'); 
    background: #fff #dddddd#125e88    #ae3;
    Walter (Elias) Disney: #f51f51
    background: -webkit-gradient(linear, from(#000000), to(#ffffff));. 

['f09', '00f', 'E4aaa0', 'fff', 'dddddd', '125e88', 'ae3', 'f51f51', '000000', 'ffffff']

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 ??

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文