How to extract all the substrings between single quotation marks in Ruby

3 Answers

0 votes
str = "I like 'Ruby', 'C', 'Python', and 'Java'."

matches = str.scan(/'([^']*)'/).flatten

p matches


# run:
# 
# ["Ruby", "C", "Python", "Java"]
#

 



answered Oct 11 by avibootz
0 votes
str = "I like 'Ruby', 'C', 'C#', 'Python', and 'Java'."

matches = str.scan(/'([^']*)'/).map(&:first)

p matches


# run:
# 
# ["Ruby", "C", "C#", "Python", "Java"]
#

 



answered Oct 11 by avibootz
0 votes
text = <<~TEXT
  User selected 'apple'
  Then selected 'orange'
  Finally selected 'banana'
TEXT

results = text.scan(/'([^']*)'/).flatten

puts results


# run:
# 
# apple
# orange
# banana
#

 



answered Oct 11 by avibootz

Related questions

...