我正在使用正则表达式来解析一个句子中的地址和时间。不同的句子变量是:
- I want to go from Cosmos Station to 525 Greenlane highway.
- I want to go from Cosmos Station to 525 Greenlane highway tomorrow at 8am.
- I want to go from Cosmos Station to 525 Greenlane highway at 8am.
我希望用一种简单的方法来解决这个问题,得到一个介于from和to之间的文本,并假设它是一个原点等等。你知道吗
from(.*)to(*.)
走这条路对吗?我想提取的起源,目的地和时间。预期结果是:
Origin = cosmos station
Destination = 525 Greenlane Highway
remaining_string = none if sentences ends at destination
remaining_string = text after destination
你可以看看我的解决方案in a live online demo at regex101.com。你知道吗
有三个命名的捕获组,每个捕获组对应一个目标变量。你知道吗
您将注意到在Time capture组中,我有
(tomorrow|at)
,它用于匹配时间子字符串的时间起始字。你知道吗虽然这适用于您的特定问题,但必须对所有其他可能检查的时间值进行扩展。你知道吗
如果我们不知道我们可以或不能做出什么样的假设,那么很难做出一个能够捕获所有边缘情况的正则表达式,所以请随意发布完整的预期输入集。你知道吗
这项工作针对给定的样本:
输出:
正如您在第一组中看到的,第三个位置是空字符串,因为没有时间。这个键是正向的lookahead
.+?(?=\s{at})
,它不会占用时间部分,但是它会在(?:\s{at}(.+))?
之前返回。你知道吗相关问题 更多 >
编程相关推荐