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.
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
\$
Match a $
char0*
Match optional zeroes(
Capture group 1
[^0$\s]+
Match 1+ times any char except 0
$
or a whitespace char)
Close group 1re = /\$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