"Assaph Mehr" <assaph@gmail.com> schrieb im Newsbeitrag
news:1111629894.417238.111830@l41g2000cwc.googlegroups.com...
> s = '0123456789'
> s.scan(/\d\d/) #-> ["01", "23", "45", "67", "89"]
>
> Now I want to exclude "45".
> How can I express it in the regex?
> When it's only one character, I can use ^.
> But for 2 characters, I don't think I can use it.
>
> What I want is:
>
> s = '0123456789'
> s.scan(some_regex) #-> ["01", "23", "67", "89"]
Negative lookahead:
s.scan /(?!4|5)\d\d/
Note the OR sign ('|') between the digits, otherwise it would produce:
["01", "23", "56", "78"]
But:
s = '01234567894657'
=> "01234567894657"
s.scan /(?!4|5)\d\d/
=> ["01", "23", "67", "89", "65"]
s.scan /\d\d/
=> ["01", "23", "45", "67", "89", "46", "57"]
IOW, you loose "46" and "57".
I prefer a non RE solution in these cases as it's simpler
s.scan(/\d\d/).reject {|x| "45" == x}
=> ["01", "23", "67", "89", "46", "57"]
Otherwise RE becomes really complex if you want to make it right - if it's
possible at all (see other postings).
Kind regards
robert