【发布时间】:2016-09-14 18:34:33
【问题描述】:
我想使用正则表达式获取 IP 地址的path[2]。对身份验证访问控制很有用。
var a = "10.56.7.234";
var x = /\d+\.(\d+)\./;
WScript.Echo(a.replace(x,"")+" split "+a.split(x)[0]+";"+a.split(x )[1]);
如何设置 x 以获得 56 作为结果?
((new Regex("(\\d+)")).Match("10.56.7.234")).Value?Windows 7 会在拆分时生成异常。
int ip =
int.TryParse(Request.ServerVariables["REMOTE_ADDR"].Split('.')[1],out ip)
? ip : ip;
/* generate exception, and regexp is not generate exception */
【问题讨论】:
-
/\d+\.(\d+)\./.exec("10.56.7.234")[1] -
简单拆分怎么样:
"10.56.7.234".split('.')[1]? -
@Wiktor Stribiżew 谢谢,我不明白为什么 replace 和 split 没有给出结果,但 exec 获得了很好的结果。我有一个问题:Сan regexp get result from any label to any label,例如只有IP地址的2号?
-
你需要为每个数字指定上下文,否则你会得到第一个。在你的情况下,使用
split("."),它更容易。 -
变体字符串之一 s=(((new Regex(@"\.(\d+\.)")).Match("10.23.45.678"))).ToString() 得到 . 23.
标签: javascript c# regex