Strange regex

  • Автор темы Автор темы aleksa77
  • Дата начала Дата начала

aleksa77

Client
Регистрация
30.09.2011
Сообщения
914
Реакции
90
Баллы
28
18d748dc2b38bd49d613181478270db4.png


This is url
/v-cars-trucks/calgary/2004-chevrolet-venture-value-plus-minivan-van/1129900634?enableSearchNavigationFlag=true

my target is 1129900634 , you will see, i put after search text ?enableSearch , and put before / , and check option " shortest match", but system give me allways full url ?
 
Ok, i put diferent regex to give me numbers- [0-9]{10}([0-9]{12})? , but still not understand why this first regex not work, why not take shortest match
 
It works like it should. Regex search goes from left to right.
 
But same result is with shortest match and without ?
 
Yes. It finds something that match (?<=/) this part and continue to search untill it finds the end of regex.
 
  • Спасибо
Реакции: aleksa77
.* is a wildcard, so you're searching for something that begins with / and ends with ?enableSearch, which your result does.

(?<=/)\d{10} : Looks for a match that starts with / followed my 10 numbers.
(?<=/)\d{10}(?=\?enableSearch) : Looks for a match that starts with / followed by 10 numbers and then followed by ?enableSearch.

 
  • Спасибо
Реакции: aleksa77
Just do a \d+

And then grab the element you need :)
 
Just do a \d+
And then grab the element you need :-)
It might work in this case, but I wouldnt't make a habit of using such a wide searches.
In this case you can just take the LAST match from your regex, but for longer and more complex urls it could give you the wrong result.

Example, say you want the year from the url instead (/v-cars-trucks/calgary/2004-chevrolet-venture-value-plus-minivan-van/1129900634?enableSearchNavigationFlag=true)


In the first url it's the first match (2 total matches), but in the second url it's the second match (3 total matches).

Instead you could make it more strict, with for example (?<=/)(19|20)\d{2}
It will look for a 4 length number after a slash. First 2 being 19 or 20, followed by 2 x 0-9.
So it would match 1900-2099.



Or (?<=/)(19|20)\d{2}(?=[^\d]), it will work as the one above but also make sure there's not a 5'th number following the first 4.



See how it doesn't match 2015 in the second url? (/2016-some-bmw-model/20151?enableSearch)

Here's a little cheat sheet I use from time to time, bookmark it. http://tobbe.co/regex.php
 
Последнее редактирование:

Кто просматривает тему: (Всего: 0, Пользователи: 0, Гости: 0)