日期:2014-05-18  浏览次数:20370 次

50分求正则表达式截取字符串
各位高手,我只想截取以下字符串中bobell,就是只取userid的值(长度任意)

<TD> <A   href= "bbsqry?userid=bobell "> bobell </A>
        <TD> May   11
        <TD> 103345
        <TD> <A  
            href= "bbsfexp2?fid=2&amp;board=LoveBridge "> more  
</A>

------解决方案--------------------
string str = " ";
string value = Regex.Match(str, @ " <td> \s* <a\s*href=\ " "bbsqry\?userid=(? <id> \w+\ " ")[^> ]*> ", RegexOptions.IgnoreCase).Groups[ "id "].Value;
------解决方案--------------------
string str = Regex.Match(strInput, @ " <a\s+href=.*?userid=(? <userid> \w+) ",RegexOptions.IgnoreCase).Groups[ "userid "].Value;

------解决方案--------------------
sorry.上面那个\ " " 放错位置了.

string str = " ";
string value = Regex.Match(str, @ " <td> \s* <a\s*href=\ " "bbsqry\?userid=(? <id> \w+)\ " "[^> ]*> ", RegexOptions.IgnoreCase).Groups[ "id "].Value;
------解决方案--------------------
如果能保证userid在你的源字符串中唯一,这样就可以

string yourStr = ........;
string resultStr = " ";
Match m = Regex.Match(yourStr, @ "userid=(? <id> [^ " " '\s> ]*) ", RegexOptions.IgnoreCase);
if (m.Success)
{
resultStr = m.Groups[ "id "].Value;
}

如果不能保证唯一,那这样

string yourStr = ......;
string resultStr = " ";
Match m = Regex.Match(yourStr, @ " <a\s+href=([ " " ']?)[^ " " '\s]*?userid=(? <id> [^ " " '\s> ]*)\1?[^> ]*> ", RegexOptions.IgnoreCase);
if (m.Success)
{
resultStr = m.Groups[ "id "].Value;
}
------解决方案--------------------
高手啊
------解决方案--------------------
mark