Thursday, April 22, 2010

Re: regex for a range that has a given number?

Hi Tim!

On Do, 22 Apr 2010, Tim Chase wrote:

> Not that I know of. To tackle the problem, I'd likely
> create a search-function that looks for matches of the form

That's what I'd have done to solve this problem.

> function! FootSearch(fn)
> let re='\[\(\%(\d\+\%(-\d\+\)\=,\)*\%(\d\+\%(-\d\+\)\=\)]\)'
> let newmatch=searchpos(re, 's')
> let initialmatch=newmatch
> while newmatch != [0,0]
> let [r,c]=newmatch
> " m = the contents of the match
> " with the "[" and "]" stripped off
> " using the "[1:-2]"
> let m=matchstr(getline(r)[c:], re)[1:-2]
> for bit in split(m, ',')
> " deal with any comma-separated list
> " of pages or ranges
> if bit=~'.*-.*'
> " if it's a page-range
> " pull the start/end
> let [st, end]=split(bit, '-')
> if st <= fn && fn <= end
> " we're between the pages in that range
> return
> endif
> else
> if bit == fn
> " it's an exact match
> " might want do strip leading zeros
> " from bit in case you search for "4"
> " and the text contains [04]
> return
> endif
> endif
> endfor
> " if we've not returned by now,
> " this footnote doesn't match
> " so find the next and keep going
> let newmatch=searchpos(re, 's')
> if newmatch == initialmatch
> " we've found the first match again
> " so we've likely looped around
> " and we've not returned yet
> " so prevent an infinite loop
> break
> endif
> let [r,c]=newmatch
> endwhile
> endfunction

Oh, you already provided a solution. Then I don't need to spend time on
that one.

>
> Any bugs are yours to keep ;-)

This is one I noticed:
:s/\%(fu.*\)\@<!fn/a:&/g

> But that's the general idea I'd use.

Mine was similar.

regards,
Christian

--
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

Subscription settings: http://groups.google.com/group/vim_use/subscribe?hl=en

No comments: