gpt4 book ai didi

正则表达式:提取由特定单词分隔的单词

转载 作者:行者123 更新时间:2023-12-02 02:04:41 25 4
gpt4 key购买 nike

这些是输入字符串:
第 1 行:item1 199-30, 1/1/2021
第 2 行:item2 29 minus 5, 1/1/2021

我想使用正则表达式来提取,如下所示:
第 1 行:19930
第 2 行:295

我尝试过,这是我的正则表达式:
([\d]+)[-|(\s*减\s*)]([\d]+)

仅匹配第 1 行。

最佳答案

根据您显示的示例,请尝试以下正则表达式。

^item\d+\s+(\d+)(?:-|\s+minus)\s*(\d+)

Online demo for above regex

说明:为上述内容添加详细说明。

^item\d+\s+        ##Matching item from starting of value followed by 1 or more digits followed by 1 or more space occurrences.
(\d+) ##Creating 1st capturing group which has digits in it.
(?:-|\s+minus)\s* ##In a non-capturing group matching either - OR 1 or spaces followed by minus. Matching 0 or more spaces.
(\d+) ##Creating 2nd capturing group which has digits in it.

关于正则表达式:提取由特定单词分隔的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68586797/

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