Reputation: 1924
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
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
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
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