Max Rose-Collins
Max Rose-Collins

Reputation: 1924

Ruby regex extract words between { | | }

How can I get the individual words contained within {} out of the text

an example of the text {Creating|Making|Producing} blah blah blah

I have got this far with my limited regex knowledge

text.scan(/{([^}]*)}/)

This just gives me {Creating|Making|Producing} but I want Creating Making Producing

Thank you!

Upvotes: 3

Views: 347

Answers (4)

Arup Rakshit
Arup Rakshit

Reputation: 118271

Another one :

s = 'an example of the text {Creating|Making|Producing} blah blah blah'
s.scan(/(?<=[|{])[A-Za-z]+(?=[}|])/)
# => ["Creating", "Making", "Producing"]

(?<=pat) :

Positive lookbehind assertion: ensures that the preceding characters match pat, but doesn't include those characters in the matched text

(?=pat) :

Positive lookahead assertion: ensures that the following characters match pat, but doesn't include those characters in the matched text

Look in Rubular also.

Update As per the comment of @Mike Campbell .

s = 'an example of the text {Creating|Making|Producing} blah {foo} blah |bla|'
s.scan(/(?<={)[a-z|]+(?=})/i).flat_map { |m| m.split("|") }
# => ["Creating", "Making", "Producing", "foo"]

Again see the Rubular.

Upvotes: 2

Bala
Bala

Reputation: 11244

s = 'an example of the text {Creating|Making|Producing} blah blah blah'

s.scan(/(?<={).*(?=})/).map{|i| i.split("|") }.flatten  
# => ["Creating", "Making", "Producing"]

s.scan(/(?<={).*(?=})/).first.split("|")    
# => ["Creating", "Making", "Producing"]

Upvotes: 0

Jonas Elfstr&#246;m
Jonas Elfstr&#246;m

Reputation: 31428

Instead of a regular expression you could simply skip the first and last characters.

str = "{Creating|Making|Producing}"
str[1..-2].split('|')
=> ["Creating", "Making", "Producing"]

Upvotes: 1

rid
rid

Reputation: 63472

You could split the found match.

text.scan(/{([^}]*)}/)[0][0].split('|')

An easier regex could be:

text.scan(/{(.*?)}/)

Explanation:

  • { - a { character
  • .*?} - anything (.*) until the first (?) } character is encountered

Upvotes: 3

Related Questions