Advertisement
n8henrie

SearchLink Regex Bug

Aug 9th, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.97 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3. input = "[Brett](http://brettterpstra.com/) can't replicate [the](!g) issue."
  4.  
  5. input.gsub!(/\[(.*?)\]\((\!.+?)\)/) do |match|
  6.     link_text = $1
  7.     link_info = $2
  8.     puts "link_text:"
  9.     puts link_text
  10.     puts "link_info:"
  11.     puts link_info
  12.     puts "\n"
  13. end
  14.  
  15. input = "[Brett](http://brettterpstra.com/) can't replicate [the](!g) issue."
  16.  
  17. # Remove the `\!` from the regex
  18. input.gsub!(/\[(.*?)\]\((.+?)\)/) do |match|
  19.     link_text = $1
  20.     link_info = $2
  21.  
  22. # Only match links starting with `!`
  23.     if link_info[0,1] == '!'
  24.         puts "This was a SearchLink link:"
  25.         puts link_text
  26.         puts link_info
  27.         puts "\n"
  28.     else
  29.         puts "This was not a SearchLink link:"
  30.         puts link_text
  31.         puts link_info
  32.         puts "\n"
  33.     end
  34. end
  35.  
  36. # Output:
  37. # link_text:
  38. # Brett](http://brettterpstra.com/) can't replicate [the
  39. # link_info:
  40. # !g
  41. #
  42. # This was not a SearchLink link:
  43. # Brett
  44. # http://brettterpstra.com/
  45. #
  46. # This was a SearchLink link:
  47. # the
  48. # !g
  49. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement