I am using the form of gsub that takes a block to determine what to
substitute.
My problem is that I can’t quite get the regex working, but I will be
able to detect that it matched incorrectly, once I can inspect the
backreferences in the block.
So: if I determine via code in the substitution block that I don’t want
to do the substitution, is there a way to simply abandon the gsub call
on that particular iteration?
I am using the form of gsub that takes a block to determine what to
substitute.
My problem is that I can’t quite get the regex working, but I will be
able to detect that it matched incorrectly, once I can inspect the
backreferences in the block.
So: if I determine via code in the substitution block that I don’t want
to do the substitution, is there a way to simply abandon the gsub call
on that particular iteration?
I am using the form of gsub that takes a block to determine what to
substitute.
My problem is that I can’t quite get the regex working, but I will be
able to detect that it matched incorrectly, once I can inspect the
backreferences in the block.
So: if I determine via code in the substitution block that I don’t want
to do the substitution, is there a way to simply abandon the gsub call
on that particular iteration?
Just return the original:
“abcdef”.gsub(/./) {|s| if s == ‘e’ then s else ‘z’ end }
=> “zzzzez”
I am using the form of gsub that takes a block to determine what to
substitute.
My problem is that I can’t quite get the regex working, but I will be
If you provide more detail about the input and the text that you want
to match we might be able to help fix the regular expression. IMHO
that approach is superior to simply returning the match effectively
replacing it with itself (which does work of course).
If you provide more detail about the input and the text that you want
to match we might be able to help fix the regular expression. IMHO
that approach is superior to simply returning the match effectively
replacing it with itself (which does work of course).
self.html.gsub!(/<a\s+?[^>]?href=([’"]) #<a up to and including
href=’ or href="
(?!mailto:)(.?) #Contents of any non-mailto:
href attribute
\1.?> #End of href attribute (same
quote) + arbitrary text to end of opening tag
(.?) #Contents of - the “link
display”
<\?/a>/mix) { #Closing tag, allowing
for optional , e.g. or </a>
irb(main):024:0> t.scan %r{
irb(main):025:0/ <a(?:\s+\w+=["’][^"’]["’])> # opening tag
irb(main):026:0/ (?:.(?!<a))*? # between and
irb(main):027:0/ # closing tag
irb(main):028:0/ }mix
=> ["<a href=“foo”>+++"]
The trick is to have a negative lookahead assertion on each character
between the beginning and ending sequence. Thus avoiding a match if the
opening sequence appears anywhere in between.