我有一个如下所示的字符串模式,
=> "text1 http://www.domain.com/experiment_B_setup_diagram_image.png\n\nExperiments text2 http://www.domain.com/experiment_C_setup_diagram_image.png \nexperiment text3 http://www.domain.com/experiment_A_plotted_cha
rt.png"
我想要分开url然后逐个字符串,输出应该是,
> ["text1",
> "http://www.domain.com/experiment_B_setup_diagram_image.png",
> "\n\nExperiments", "text2",
> "http://www.domain.com/experiment_C_setup_diagram_image.png",
> "\nexperiment", "text3",........]
像单独的url及其前面或后续的字符串作为数组...有人可以帮助我吗?
答案 0 :(得分:2)
尝试使用:
.split(/\s|\n+/)
例如:
text="text1 http://www.domain.com/experiment_B_setup_diagram_image.png\n\nExperiments text2 http://www.domain.com/experiment_C_setup_diagram_image.png \nexperiment text3 http://www.domain.com/experiment_A_plotted_chart.png"
text.split(/\s|\n/)
返回:
["text1", "http://www.domain.com/experiment_B_setup_diagram_image.png", "", "Experiments", "text2", "http://www.domain.com/experiment_C_setup_diagram_image.png", "", "experiment", "text3", "http://www.domain.com/experiment_A_plotted_chart.png"]
答案 1 :(得分:1)
只要您放弃换行符(\ n)就可以了,您可以先将它们转换为空格,然后简单地拆分空格:
x = "text1 http://www.domain.com/experiment_B_setup_diagram_image.png\n\nExperiments text2 http://www.domain.com/experiment_C_setup_diagram_image.png \nexperiment text3 http://www.domain.com/experiment_A_plotted_chart.png"
x.tr("\n", " ").split(" ")