problem with "shortest match" in regex

archel

Client
Регистрация
02.05.2011
Сообщения
175
Благодарностей
22
Баллы
18
Hi

Normally I don't have problems with regex but I think I misunderstand the "shortest match" = ? operator.

Let's say you have a string:
1-2-3-4|5
And I want to extract "4".

I though it's easy: "-" is before 4 and "|" after 4 ... and then you"ll get 4 if you use the shortest match operator. So:
(?<=-).*?(?=\|)

Though, this doesn't extract "4", but "2-3-4", which means .*? doesn't extract the shortest match(since "4" and even "3-4" would be shorter)...although it should.? (unless I misinterpret the ? operator).

Is this a bug or is my regex wrong?
Thanks.
 

bigcajones

Client
Регистрация
09.02.2011
Сообщения
1 216
Благодарностей
683
Баллы
113
\d+(?=\|) will give you 4.
 

archel

Client
Регистрация
02.05.2011
Сообщения
175
Благодарностей
22
Баллы
18
Yes of course, but the numbers are only for the example...4 could be any non-operator character but - or |.
So, I need something like .*
Anybody knows why (?<=-).*?(?=\|) doesn't work?
 

bigcajones

Client
Регистрация
09.02.2011
Сообщения
1 216
Благодарностей
683
Баллы
113
\w+(?=\|) will give you a word if that is what you are looking for. The shortest match is for something where you have 2 of the same character, line or number in the string you are looking for and need to stop the regex at the first one.
 
  • Спасибо
Реакции: archel

archel

Client
Регистрация
02.05.2011
Сообщения
175
Благодарностей
22
Баллы
18
O I see, misenterpreted "shortest match", thought it takes the option with the shortest amount of characters.
Thanks. I'll look for another solution.
 

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