regex - Extract links from BBcode with Ruby -
which simple method / regular expression extract links bbcode [code]...[/code] section? links begin http:// , end \n or [/code] tag, maybe space or other whitespace characters @ end.
one [code] section can contains multiple links / code tag:
[code]http://example1.com http://example2.com http://example3.com [code]
and multiple consecutive [code] sections can occur:
[code]http://example4.com http://example5.com [/code] [code]http://example6.com[/code] [code] http://example7.com http://example8.com[/code]
i links such section defined above in simple flattened array, unable solve right regular expression scan method.
try one:
data = '[code]http://example4.com http://example5.com [/code] [code]http://example6.com[/code] [code] http://example7.com http://example8.com[/code]' p data.split(/\[\/*code\]/) .flat_map{|el| el.split(/\s+/)} .reject(&:empty?)
output:
#=> ["http://example4.com", "http://example5.com", "http://example6.com", "http://example7.com", "http://example8.com"]
Comments
Post a Comment