-4

我想知道用其中的 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 是

值= &quot;vid_id= 100226&amp;

谢谢你的帮助

4

2 回答 2

2

使用这个正则表达式:

vid_id=(\d+)
于 2012-03-24T21:09:15.780 回答
1

您可以使用这样的正则表达式:

(?<=vid_id=)\d+

这使用正向后向匹配以文字字符集“vid_id=”开头的任意数量的数字

这里又是 RegexBuddy 评论:

@"
(?<=          # 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)
"

你已经用两者标记了你的问题,php所以asp.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;...");
于 2012-03-24T22:44:57.897 回答