【问题标题】:using regular expression for preg replace [closed]使用正则表达式进行 preg 替换 [关闭]
【发布时间】:2012-04-08 23:32:02
【问题描述】:

我想知道用其中的 vid_id 替换这个字符串的最佳正则表达式

$code = '&lt;object width=&quot;420&quot; height=&quot;345&quot;&gt;<br />
    &lt;param name=&quot;movie&quot; value=&quot;http://www.badoobook.com/clips/swf/player.swf&quot;&gt;&lt;/param&gt;<br />
    &lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;flashvars&quot; value=&quot;vid_id=100226&amp;MainURL=http%3A%2F%2Fwww.bado  obook.com%2Fclips&amp;em=1&quot;&gt;<br />
    &lt;embed src=&quot;http://www.badoobook.com/clips/swf/player.swf&quot; flashvars=&quot;vid_id=100226&amp;MainURL=http%3A%2F%2Fwww.  badoobook.com%2Fclips&amp;em=1&quot; type=&quot;application/x-shockwave-flash&quot; allowScriptAccess=&quot;always&quot; width=&quot;420&quot; height=&quot;345&quot; allowFullScreen=&quot;true&quot;&gt;&lt;/embed&gt;&lt;/object&gt;'
$regular = '';
$code = preg_replace($regular ,'$1' , $code);
echo $code;

这段代码中的vid id是

value=&amp;quot;vid_id=100226&amp;amp;

感谢您的帮助

【问题讨论】:

  • 我面临的最大问题是 <我认为regulat表达式中有一些特殊的东西可以阅读它,我不知道它

标签: php asp.net regex regular-language


【解决方案1】:

你可以像这样使用正则表达式:

(?<=vid_id=)\d+

这使用positive lookbehind 来匹配前面有文字字符集“vid_id="

的任意数量的数字

这里又是 RegexBuddy cmets:

@"
(?<=          # Assert that the regex below can be matched, with the match ending at this position (positive lookbehind)
   vid_id=       # Match the characters “vid_id=” literally
)
\d            # Match a single digit 0..9
   +             # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
"

您已使用phpasp.net 标记了您的问题,所以我不确定您要执行哪种实现。两者都有:

Asp.Net (C#)

Regex.Replace(@"...uot;vid_id=100226&amp;...", @"(?<=vid_id=)\d+", "[Your value here]")

php

echo preg_replace("(?<=vid_id=)\d+", "[Your value here]", "...uot;vid_id=100226&amp;...");

【讨论】:

    【解决方案2】:

    使用这个正则表达式:

    vid_id=(\d+)
    

    【讨论】:

    • 这会给我 id ,但我想要的是用 nember 替换整个代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多