regexrubular

Rubular Expression


How can we extract the words from a string in rubular expression?

$0Public$robotics00$india0$000facebook

If we want to extract the words Public robotics india facebook from the above string, how can we?

I am using ([^0$]), but it is giving the letters not the proper words.


Solution

  • You can match $ followed by optional zeroes, and use a capture group to match the characters other than $ 0 or a whitespace char

    \$0*([^0$\s]+)
    

    Explanation

    Regex demo

    re = /\$0*([^0$\s]+)/
    str = '$0Public$robotics00$india0$000facebook
    '
    
    # Print the match result
    str.scan(re) do |match|
        puts match.first
    end
    

    Output

    Public
    robotics
    india
    facebook