gpt4 book ai didi

javascript - 正则表达式组结果和或

转载 作者:行者123 更新时间:2023-11-29 19:13:15 24 4
gpt4 key购买 nike

是否可以通过两个重要的正则表达式检查返回一个数组?

类似这样的东西 - /(?=.*\d days)(?=.*car|truck|motorcycle)/

第一个问题是正则表达式不匹配汽车或其他车辆...其次 - 是否可以将天数 ...*\d days(... 提取到单独的数组值中?

目前我的正则表达式是 - /(?=.*\d days)(?=.*car)/响应是 - [ '', index: 0, input: '5 days before cleaning the car' ]我正在争取 - [ '5', index: 0, input: '5 days before cleaning the car']

输入

5 days before cleaning a car 
Motorcycle cleaning in 23 days
truck will arrive in 10 days

基本上我想获取短文本中可能存在的任何车辆的天数。

最佳答案

描述

^(?=.*?([0-9]+)\s*days)(?=.*?(motorcycle|truck|car)).*

Regular expression visualization

此正则表达式将执行以下操作:

  • 记录天数
  • 捕获车辆类型

例子

现场演示

https://regex101.com/r/qY5nL6/1

示例文本

5 days before cleaning a car
Motorcycle cleaning in 23 days
truck will arrive in 10 days

样本匹配

[0][0] = 5 days before cleaning a car
[0][1] = 5
[0][2] = car

[1][0] = Motorcycle cleaning in 23 days
[1][1] = 23
[1][2] = Motorcycle

[2][0] = truck will arrive in 10 days
[2][1] = 10
[2][2] = truck

说明

NODE                     EXPLANATION
----------------------------------------------------------------------
^ the beginning of a "line"
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
----------------------------------------------------------------------
days 'days'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
motorcycle 'motorcycle'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
truck 'truck'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
car 'car'
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------

关于javascript - 正则表达式组结果和或,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37246748/

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