You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:将设备型号与版本号的两个正则捕获组合并为单个匹配表达式

正则合并问题:匹配设备型号与对应版本号

目标字符串

// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

main  = {
data1 = value
data2 = value
model_types = [
// Type 1,2,3
{
device_model = ['type1_v1', 'type1_v2']
current_version = '1.2.34.56'
},
{
device_model = ['type2_v1', 'type2_v2']
data3 = value
current_version = 'type2911-54.32'
},
{
device_model = ['type_v3']  // misc data
current_version = '1.2.3-t35_098'
},

// Type 4 and 5
{
device_model = ['type4_v1', 'type4_v2']
data4 = value
data5 = value
current_version = '6.6.9-TY4_998'
},
{
device_model = ['type5_v1', 'type5_v2', 'type5_v3']
data6 = value
current_version = '1.5.6-TY5_354'
data7 = value
},

// Type 6 and Wifi1
{
device_model = ['type6_v1']
current_version = '1.4.65-20240220'
},
{
device_model = ['type6_w1']
current_version = '1.2.3'
},

// Type 7 and Wifi2
{
device_model = ['type7_v1']
data8 = value
current_version = '2.3.6-9'
children = [{
data9 = ['value']
current_version = '4.8.1'
}]
},
{
device_model = ['type7_w2']
data10 = value
current_version = '5.1.4-b'
children = [{
data9 = ['value']
current_version = '8.4.1'
}]
},

// Type 8
{
device_model = ['type8_v1']
current_version = 'ty8-87.41'
data11 = value
},
{
device_model = ['type8_v2']
current_version = 'ty8-87.41'
},

// Type 9
{
device_model = ['type9_v1]
current_version = 'ty9-14.9-v198'
},

// Type 10
{
device_model = ['type10_v1', 'type10_v2']
current_version = '2.2.3.110ty'
},
]
}

现有独立正则

  • 设备型号匹配正则(包含device_model和data9字段):
    (?:device_model = \['?|data9 = \['|, ')([^'\],\r\n]*)(?=.*\r?\n(?:[ \t]*\[.*\r?\n)*?[ \t]*)
    
  • 版本号匹配正则:
    (?:current_version = ')([^'\],\r\n]*)(?=.*\r?\n(?:[ \t]*\[.*\r?\n)*?[ \t]*)
    

错误的合并尝试

尝试合并成单个正则以同时捕获设备型号和对应版本号,但表达式报错“Your regular expression does not match the subject string”:

(?:device_model = \['?|data9 = \['|, ')([^'\],\r\n]*)(?=.*\r?\n(?:[ \t]*\[.*\r?\n)*?[ \t]*\[(?::current_version = ')(.*))

修正方案

你的合并正则有两处关键错误:

  1. 冗余添加了\[和::,导致无法匹配到current_version行
  2. 正向预查的逻辑没有正确关联到目标版本号的捕获

正确的合并正则如下:

(?:device_model = \['?|data9 = \['|, ')([^'\],\r\n]*)(?=.*\r?\n(?:[ \t]*.*\r?\n)*?[ \t]*current_version = '([^'\],\r\n]*))

修改说明

  • 移除了错误的\[和::,直接匹配current_version = '前缀
  • 调整预查内的匹配逻辑,用(?:[ \t]*.*\r?\n)*?非贪婪匹配设备型号行和版本号行之间的任意内容
  • 第二个捕获组([^'\],\r\n]*)直接捕获版本号内容,确保与对应设备型号关联

该正则会为每个设备型号(包括data9中的值)返回两个捕获组:组1是设备型号,组2是对应的版本号。


内容的提问来源于stack exchange,提问作者snicksnackpaddywhack91

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 17:01:16