Strange regex

aleksa77

Client
Регистрация
30.09.2011
Сообщения
914
Благодарностей
90
Баллы
28


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 ?
 

aleksa77

Client
Регистрация
30.09.2011
Сообщения
914
Благодарностей
90
Баллы
28
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
 

rostonix

Известная личность
Регистрация
23.12.2011
Сообщения
29 067
Благодарностей
5 715
Баллы
113
It works like it should. Regex search goes from left to right.
 

aleksa77

Client
Регистрация
30.09.2011
Сообщения
914
Благодарностей
90
Баллы
28
But same result is with shortest match and without ?
 

rostonix

Известная личность
Регистрация
23.12.2011
Сообщения
29 067
Благодарностей
5 715
Баллы
113
Yes. It finds something that match (?<=/) this part and continue to search untill it finds the end of regex.
 
  • Спасибо
Реакции: aleksa77

Tobbe

Client
Регистрация
01.08.2013
Сообщения
428
Благодарностей
148
Баллы
43
.* 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

Morgan

Пользователь
Регистрация
30.11.2015
Сообщения
118
Благодарностей
12
Баллы
18
Just do a \d+

And then grab the element you need :-)
 

Tobbe

Client
Регистрация
01.08.2013
Сообщения
428
Благодарностей
148
Баллы
43
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
 
Последнее редактирование:

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