gpt4 book ai didi

regex - 如何在 Rebol 中查找与模式匹配的所有子字符串

转载 作者:行者123 更新时间:2023-12-01 07:56:36 24 4
gpt4 key购买 nike

在这里,我试图在字符串中查找模式的所有匹配项:

theString: "There is a blue truck and a red car next to an orange building."
thePattern: [["blue" | "red" | "orange"] ["truck" | "car" | "building"]]
print parse thePattern theString

而不是返回 ["red truck" "blue car" "orange building"] , parse函数返回 false .

Rebol 有没有类似其他编程语言的正则表达式匹配函数,可以用来查找字符串中某个模式的所有匹配的函数?

最佳答案

你可以试试这个:

string: "There is a blue truck and a red car next to an orange building."
pattern: [
["blue" | "red" | "orange"]
space
["truck" | "car" | "building"]
]

parse string [
some [
copy value pattern (print value)
| skip
]
]

打印:
blue truck
red car
orange building
skip用于在模式不匹配时移动到下一个字符。由于它不是“bluetruck”或“redcar”,因此向模式添加了空间。

括号用于执行解析规则内的 Rebol 代码,因此可以替换 print还有其他东西(比如 append block value 等)

关于regex - 如何在 Rebol 中查找与模式匹配的所有子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24025696/

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