gpt4 book ai didi

ruby - 我怎样才能拆分这个字符串?

转载 作者:数据小太阳 更新时间:2023-10-29 07:23:59 25 4
gpt4 key购买 nike

我有这个字符串:

a = "hy what are you doing [Zoho Reports] will you like? [Zoho Books] reply"

我想拆分它,所以结果是这样的:

hy
what
are
you
doing
[Zoho Reports]
will
you
like?
[Zoho Books]
reply

我如何循环该字符串以获得这些结果?我目前正在这样做:

a.split("")

但它将 [Zoho Reports]" 拆分为 "[Zoho""Reports]",我不知道想要。

最佳答案

在这种情况下,您应该使用scan 而不是split,因为它更容易表征您想要什么而不是您想要扔掉什么。

类似于 Bozhidar 的回答,但您不需要复杂化。

a.scan(/\[.*?\]|\S+/)

关于ruby - 我怎样才能拆分这个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12771387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com