Can a repeated piece of regular expression create multiple groups? Such as this example...

Posted by Yousui on Stack Overflow See other posts from Stack Overflow or by Yousui
Published on 2010-04-06T14:45:08Z Indexed on 2010/04/06 14:53 UTC
Read the original article Hit count: 236

Filed under:
|
|

Hi guys,

I'm using RUBY 's regular expression to deal with text such as

${1:aaa|bbbb}
${233:aaa | bbbb | ccc  ccccc }
${34: aaa | bbbb | cccccccc     |d}
${343:   aaa   |   bbbb   |       cccccccc     |dddddd   ddddddddd}
${3443:a aa|bbbb|cccccccc|d}
${353:aa a| b b b b | c c c c c c c c      |        dddddd}

I want to get the trimed text between each pipe line. For example, for the first line of my upper example, I want to get the result aaa and bbbb, for the second line, I want aaa, bbbb and ccc ccccc. Now I have wrote a piece of regular expression and a piece of ruby code to test it:

array = "${33:aaa|bbbb|cccccccc}".scan(/\$\{\s*(\d+)\s*:(\s*[^\|]+\s*)(?:\|(\s*[^\|]+\s*))+\}/)
puts array

Now my problem is the (?:\|(\s*[^\|]+\s*))+ part can't create multiple groups. I don't know how to solve this problem, because the number of text I need in each line is variable. Anyone can help? Great thanks.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about regex