Search code examples
rubyregexrubular

Regexp, strings between string


So string looks like that:

 abc_#xoxo#_xyz

I want to pull out everyting except _#*#_ and get them in two match results (abc and xyz). I made a regexp to get stuff from inside:

 (?<=_#)[^}]*(?=#_)

I have struggled with it for quite a while and have no idea how to catch that, suggestions?


Solution

  • If I understood your problem correctly, this is a very easy task.

    (.*)_#.*#_(.*)
    

    Link: http://rubular.com/r/iymHrETOlU

    Here's some ruby code

    s = 'abc_#xoxo#_xyz'
    regex = /(.*)_#.*#_(.*)/
    
    match = regex.match(s)
    match[1] # => "abc"
    match[2] # => "xyz"