我想从多行数据中捕获所有字符串。假设这里是结果,这是我的代码,不起作用。
模式:^XYZ/0-9所有的\P,我对这个部分迷失了,谁能帮上忙?
Result
XYZ/1
XYZ/1,2-5
XYZ/5,7,8-9
XYZ/2-4,6-8,9
XYZ/ALL
XYZ/P1
XYZ/P2,3
XYZ/P4,5-7
XYZ/P1-4,5-7,8-9
Changed to
XYZ/1
XYZ/1,2-5
XYZ/5,7,8-9
XYZ/2-4,6-8,9
XYZ/A12345 after the slash limited to 6 alphanumeric chars
XYZ/LH-1234567890 after the /LH- limited to 10 numeric chars
发布于 2022-03-08 13:19:22
模式可以是:
^XYZ\/(?:ALL|P?[0-9]+(?:-[0-9]+)?(?:,[0-9]+(?:-[0-9]+)?)*)$
部分匹配的模式:
^
开始XYZ\/
匹配XYX/
(您不必根据模式分隔符转义/
)(?:
外接于的捕获组ALL
匹配|
或P?
匹配可选的P
[0-9]+(?:-[0-9]+)?
将1+数字与可选的-
和1+数字相匹配。(?:
非捕获组以匹配为一个整体,[0-9]+(?:-[0-9]+)?
匹配,
和1+数字以及可选的-
和1+数字- `)*` Close the non capture group and optionally repeat it
)
关闭外部非捕获组$
末端发布于 2022-03-08 13:25:42
您可以使用这个regex模式来匹配这些行。
^XYZ\/(?:P|ALL|[0-9])[0-9,-]*$
使用全局g
和多行m
标志。
顺便说一下,[P|ALL]
和单词"ALL“不匹配。
它只匹配一个字符,即P
、A
、L
或|
。
https://stackoverflow.com/questions/71395702
复制相似问题