我有一个网站的网址,如下:
http://www.testwebsite.com/busqueda/(idPartner)/VINCCI/(codigoHotel)/20017/(zona)/74986/(lang)/es/(idPrm)/MBVINCCI/(idONg)/P69/(idNom)/VincciHoteles/(idListaHoteles)/Y/(dia)/7/(mes)/4/(anio)/2014/(diaHasta)/8/(mesHasta)/4/(anioHasta)/2014/(habitaciones)/1/(irListaHoteles)/Y/(adultsRoom1)/2/(childrenRoom1)/0
现在我想通过Regex从URL中获取20017的位置,你能否告诉我在这种情况下哪种模式最适合实现呢?
答案 0 :(得分:0)
您的字符串中有超过一年的值,但您可以执行以下正则表达式:
[^\d](\d{5})[^\d]
或只是
\/(\d{5})\/
或非常特定于此案例:
codigoHotel\/(\d{5})\/zona
然后获得第一个被捕获的组,即2007年
答案 1 :(得分:0)
只是添加到sshashank124的答案。
如果您有以下网址
string url = @"http://servername/username/collectionkey/sectionkey.extension";
//Using a Regex allows you to split the string, check that all required parts are there and check that the keys are numeric:
string servername = null, username = null, collectionkey = null, sectionkey = null;
Match m = Regex.Match(url, @"(?i:http://)(?<servername>[^\s/]*)/(?<username>[^\s/]*)/(?<collectionkey>\d*)(/(?<sectionkey>\d*))?(\.xml)?");
if (m.Success)
{
servername = m.Groups["servername"].Value;
username = m.Groups["username"].Value;
collectionkey = m.Groups["collectionkey"].Value;
sectionkey = m.Groups["sectionkey"].Value;
}