Skip to content

Instantly share code, notes, and snippets.

@Amitesh
Created August 4, 2011 06:37
Show Gist options
  • Select an option

  • Save Amitesh/1124605 to your computer and use it in GitHub Desktop.

Select an option

Save Amitesh/1124605 to your computer and use it in GitHub Desktop.
Regex with problem
1) Following regex in ruby is throwing server start (compile) time error on Mac Book Pro while on Ubuntu it is working.
# Clean all new line character
string.gsub!(/(?<!\n)\n(?!\n)/, '')
Error :
undefined (?...) sequence: /(?<!\n)\n(?!\n)/ (SyntaxError)
Solution :
string.gsub!(/\r/,"")
string.gsub!(/\n/," ")
2) named regex in ruby
s = "/system/books/248/images/1839/medium_thumb/file_name.jpg"
m = s.match(/.*\/(?<book_id>\d+)\/.*\/(?<image_id>\d+)\/.*\/.*/)
#<MatchData "/system/books/248/images/1839/medium_thumb/file_name.jpg" book_id:"248" image_id:"1839"
Retrive the results:
book_id = m[:book_id] # 248
image_id = m[:image_id] # 1839
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment