使用正则表达式替换内部链接,但如果它们是动态的,则不是,即包含php回显

时间:2015-09-25 08:26:58

标签: php regex

我正在浏览一个项目并替换内部链接以通过url函数。我创建了一些正则表达式,它可以满足我的需求,但是我不希望它对动态链接做任何事情,因为我将通过手动完成它们。

我的正则表达式是:

NSThread

我的替换是:

dispatch_async(dispatch_get_global_object(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  [self loadXML];
});

这适用于:

(href|src)=(?:'|")\/(?!http)(.+?)(?:'|")

我希望它忽略以下内容:

$1="<?php echo url('$2'); ?>"

如果某人可以提供单独的正则表达式并且替换哪个接收回声并将其置于url函数的字符串连接中,则获得积分!

的内容
<link rel='stylesheet' type='text/css' href='/public/css/print.css' media="print"/>
<link rel='stylesheet' type='text/css' href='http://example.com/public/css/print.css' media="print"/>

2 个答案:

答案 0 :(得分:0)

(?:'|")\/(?!http)([^<]+?)(?:'|")

对于第一部分,

可能已经足够好了。即,替换。使用[^&lt;]仅匹配不包含开口尖括号的字符串。

这可能适用于第二部分

(?:'|")\/(?!http)([^<]+?)(<\?php[^>]*?>)?([^<]+?)(?:'|")

答案 1 :(得分:0)

我会用这个:

(href|src)=(?:'|")\/(?!http)((?!php).)+?(?:'|")

说明:

    1st Capturing group (href|src)
        1st Alternative: href
            href matches the characters href literally (case insensitive)
        2nd Alternative: src
            src matches the characters src literally (case insensitive)
    = matches the character = literally
    (?:'|") Non-capturing group
        1st Alternative: '
            ' matches the character ' literally
        2nd Alternative: "
            " matches the characters " literally
    \/ matches the character / literally
    (?!http) Negative Lookahead - Assert that it is impossible to match the regex below
        http matches the characters http literally (case insensitive)
    2nd Capturing group ((?!php).)+?
        Quantifier: +? Between one and unlimited times, as few times as possible, expanding as needed [lazy]
        Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
        (?!php) Negative Lookahead - Assert that it is impossible to match the regex below
            php matches the characters php literally (case insensitive)
        . matches any character (except newline)
    (?:'|") Non-capturing group
        1st Alternative: '
            ' matches the character ' literally
        2nd Alternative: "
            " matches the characters "