Guest User

Untitled

a guest
Jul 15th, 2014
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.91 KB | None | 0 0
  1. # I need to parse string like that :
  2. # BEGIN STRING
  3. This is my string...
  4.  
  5. [foo-1 param1 : value1, param2 : value2]
  6. [foo-2 param1 : value1, param2 : value2, params3 : value3]
  7. # and so on
  8. # END STRING
  9. # My purpose here is to get all the sequence between [ ] firstly and then to parse all the params.
  10. # I wrote 2 regex for that, the first one is supposed to get all the sequence between [] :
  11.  
  12. /\[([\w-]+)\s?([\w@.,:\s]*)\]/m
  13.  
  14. # My second regex to parse all my parameters
  15.  
  16. /([\w]+)\s?,?:\s?,?([\w@.-]+)/m
  17.  
  18. # My problem is that I'm always getting the first match but I'd of course like to get all the matches.
  19. # I tried to do this :
  20.  
  21.  
  22. r = /\[([\w-]+)\s?([\w@.,:\s]*)\]/m
  23. str = "[foo-1 param1 : value1, param2 : value2]
  24. [foo-2 param1 : value1, param2 : value2, params3 : value3]"
  25.  
  26. m = r.match(str)
  27.  
  28. # #<MatchData "[foo-1 param1 : value1, param2 : value2]" 1:"foo-1" 2:"param1 : value1, param2 : value2">
Advertisement
Add Comment
Please, Sign In to add comment