Saturday, August 21, 2010

Re: Number ranges in vim regex

On 17/08/10 17:55, Christian Brabandt wrote:
> Hi Albert!
>
> On Mo, 16 Aug 2010, Albert wrote:
>
>> Christian Brabandt wrote:
>>> <snip>
>>> I think this should work:
>>> /\<\%([0-9]\|[^2-9][0-9]\|20\)\>
>>>
>>> Which translates roughly to
>>> \< " beginning of a word
>>> \%(...\) " Start a group
>>> [0-9] " either 0-9
>>> \| " or
>>> [^2-9][0-9] " 00 until 19
>>> \| " or
>>> 20 " 20
>>> \> " end of word
>>> <snip>
>>
>> Shouldn't the [^2-9][0-9] be [^1-9][0-9]?
>
> Because you don't want to match 10 until 19?
>
> regards,
> Christian
>

Actually, I think it should be

/\<\%(\d\|1\d\|20\)\>

(remember: \d and [0-9] are synonymous, but the former is supposed to be
(not necessarily measurably) faster. Replace the digit 1 by [01] if you
want to allow a leading zero in a two-digit decimal number. (Sometimes a
leading zero is used to mean an octal number.)

(Using [^2-9] would match the digits 0 and 1, but also any nonnumeric
character.)

For a slightly more complex range, let's say 1234 to 4567, you could use

/\<\%(123[4-9]\|12[4-9]\d\|1[3-9]\d\d\|[23]\d\d\d\|4[0-4]\d\d\|45[0-5]\d\|456[0-7]\)\>

where the or'ed parts represent respectively
1234-1239
1240-1299
1300-1999
2000-3999
4000-4499
4500-4559
4560-4567


Best regards,
Tony.
--
JOHN CLEESE PLAYED: SECOND SOLDIER WITH A KEEN INTEREST IN BIRDS, LARGE MAN
WITH DEAD BODY, BLACK KNIGHT, MR NEWT (A VILLAGE
BLACKSMITH INTERESTED IN BURNING WITCHES), A QUITE
EXTRAORDINARILY RUDE FRENCHMAN, TIM THE WIZARD, SIR
LAUNCELOT
"Monty Python and the Holy Grail" PYTHON (MONTY)
PICTURES LTD

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: