简体   繁体   中英

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!

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

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 .

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"]
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"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM