2011-10-30 41 views
3

以下几行是我需要在lua中使用的任意正则表达式。有没有办法在lua(raw)中定义自动转义的字符串?

['\";=] 
!^(?:(?:[a-z]{3,10}\s+(?:\w{3,7}?://[\w\-\./]*(?::\d+)?)?/[^?#]*(?:\?[^#\s]*)?(?:#[\S]*)?|connect (?:\d{1,3}\.){3}\d{1,3}\.?(?::\d+)?|options \*)\s+[\w\./]+|get /[^?#]*(?:\?[^#\s]*)?(?:#[\S]*)?)$ 
'(?i:(?:c(?:o(?:n(?:t(?:entsmartz|actbot/)|cealed defense|veracrawler)|mpatible(?: ;(?: msie|\.)|-)|py(?:rightcheck|guard)|re-project/1.0)|h(?:ina(?: local browse 2\.|claw)|e(?:rrypicker|esebot))|rescent internet toolpak)|w(?:e(?:b(?: (?:downloader|by mail)|(?:(?:altb|ro)o|bandi)t|emailextract?|vulnscan|mole)|lls search ii|p Search 00)|i(?:ndows(?:-update-agent| xp 5)|se(?:nut)?bot)|ordpress(?: hash grabber|\/4\.01)|3mir)|m(?:o(?:r(?:feus fucking scanner|zilla)|zilla\/3\.mozilla\/2\.01$|siac 1.)|i(?:crosoft (?:internet explorer\/5\.0$|url control)|ssigua)|ailto:craftbot\@yahoo\.com|urzillo compatible)|p(?:ro(?:gram shareware 1\.0\.|duction bot|webwalker)|a(?:nscient\.com|ckrat)|oe-component-client|s(?:ycheclone|urf)|leasecrawl\/1\.|cbrowser|e 1\.4|mafind)|e(?:mail(?:(?:collec|harves|magne)t|(?: extracto|reape)r|(siphon|spider)|siphon|wolf)|(?:collecto|irgrabbe)r|ducate search vxb|xtractorpro|o browse)|t(?:(?: ?h ?a ?t ?' ?s g ?o ?t ?t ?a ? h ?u ?r ?|his is an exploi|akeou)t|oata dragostea mea pentru diavola|ele(?:port pro|soft)|uring machine)|a(?:t(?:(?:omic_email_hunt|spid)er|tache|hens)|d(?:vanced email extractor|sarobot)|gdm79\@mail\.ru|miga-aweb\/3\.4|utoemailspider| href=)|^(?:(google|i?explorer?\.exe|(ms)?ie([0-9.]+)?\ ?(compatible(browser)?)?)$|www\.weblogs\.com|(?:jakart|vi)a|microsoft url|user-Agent)|s(?:e(?:archbot [email protected]|curity scan)|(?:tress tes|urveybo)t|\.t\.a\.l\.k\.e\.r\.|afexplorer tl|itesnagger|hai)|n(?:o(?:kia-waptoolkit.* googlebot.*googlebot| browser)|e(?:(?:wt activeX; win3|uralbot\/0\.)2|ssus)|ameofagent|ikto)|f(?:a(?:(?:ntombrows|stlwspid)er|xobot)|(?:ranklin locato|iddle)r|ull web bot|loodgate|oobar/)|i(?:n(?:ternet(?: (?:exploiter sux|ninja)|-exprorer)|dy library)|sc systems irc search 2\.1)|g(?:ameBoy, powered by nintendo|rub(?: crawler|-client)|ecko\/25)|(myie2|libwen-us|murzillo compatible|webaltbot|wisenutbot)|b(?:wh3_user_agent|utch__2\.1\.1|lack hole|ackdoor)|d(?:ig(?:imarc webreader|out4uagent)|ts agent)|(?:(script|sql) inject|$botname/$botvers)ion|(msie .+; .*windows xp|compatible \; msie)|h(?:l_ftien_spider|[email protected]|anzoweb)|(?:8484 boston projec|xmlrpc exploi)t|u(?:nder the rainbow 2\.|ser-agent:)|(sogou develop spider|sohu agent)|(?:(?:d|e)browse|demo bot)|zeus(?: .*webster pro)?|[a-z]surf[0-9][0-9]|v(?:adixbot|oideye)|[email protected]|\bdatacha0s\b|kenjin spider|; widows|rsync|\\\r))' 

而且有很多人在那里,这些来自..... 点,你可能会注意到,第一种情况下只有"是逃脱\"机器人不是' 因此,

rex_pcre.new('['\";=]') 

不起作用。

rex_pcre.new("['\";=]") 

应该工作,但是,在正则表达式的部分,如\-

我也不能使用

[[ ]] 

因为有正则表达式与](第一个例子)结束 破线在

rex_pcre.new([[ 
['\";=] 
]]) 

会为我的情况下,如不能正常工作第三个以)结尾,并且还引发了意外符号的错误。

总之我在寻找这样的Python的 r"UNESCAPED STRING"或C#的 @"UNESCAPED STRING"

..

我认为没有这样的,但不知道什么是得到一个类似的功能的方式,鉴于,我只消耗这些值(正则表达式),并就如何撰写原来他们无法控制..

这是我目前的解决方案 我只是尝试编译线,用[[ ]],如果失败了,搬到"和然后到“'”/

EscapeRegEx = function (xp) 
    -- try with [[ ]] 
    local opening = '[[' 
    local closing = ']]' 
    local codeline = "rex_pcre.new(" .. opening .. xp .. closing .. ")" 
    _, err = loadstring(codeline) 
    if not err then return codeline end 

    -- then try with " 
    opening = '"' 
    closing = '"' 
    codeline = "rex_pcre.new(" .. opening .. xp .. closing .. ")" 
    _, err = loadstring(codeline) 
    if not err then return codeline end 

    -- then try with ' 
    opening = "'" 
    closing = "'" 
    codeline = "rex_pcre.new(" .. opening .. xp .. closing .. ")" 
    _, err = loadstring(codeline) 
    if not err then return codeline end 

end 

回答

5

可以使用较长的版本的长托架的:

[=========[the regex goes in here]=========] 

开口长支架将仅由相同长度的长括号匹配。

查看this了解更多详情;你也可以做一个类似的事情来获得嵌套的多行注释。

+0

做了工作!没有意识到这[=== [游戏。 –

相关问题