Created
August 4, 2011 06:37
-
-
Save Amitesh/1124605 to your computer and use it in GitHub Desktop.
Regex with problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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