我有像10E4558AA0_String1_String2_String3_0_100_12mo
这样的字符串。我想提取string1
和string2
以及0
和100
。
0_100
也可以是0_10
,11_25
,26_75
或76_100
,我想提取0 10
,{{1}分别是},11 25
,26 75
。我认为正则表达式可以工作,但我正在努力编写部分。
答案 0 :(得分:0)
由于您的字符串由_
分隔命令,但这会因语言而异。
Javascript看起来像str.split("_")
,可以在rosettacode.org找到更多用于标记字符串的语言示例
要通过正则表达式匹配严格执行此操作,您可以添加一些字符串验证,如下面的正则表达式。
^([0-9a-f]+)_(string[^_]+)_(string[^_]+)_(string[^_]+)_([0-9]+)_([0-9]+)_([^_]+)$
现场演示
https://regex101.com/r/yT4bR4/1
示例文字
10E4558AA0_String1_String2_String3_0_100_12mo
样本匹配
[0][0] = 10E4558AA0_String1_String2_String3_0_100_12mo
[0][1] = 10E4558AA0
[0][2] = String1
[0][3] = String2
[0][4] = String3
[0][5] = 0
[0][6] = 100
[0][7] = 12mo
NODE EXPLANATION
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[0-9a-f]+ any character of: '0' to '9', 'a' to 'f'
(1 or more times (matching the most
amount possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
_ '_'
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
string 'string'
----------------------------------------------------------------------
[^_]+ any character except: '_' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
_ '_'
----------------------------------------------------------------------
( group and capture to \3:
----------------------------------------------------------------------
string 'string'
----------------------------------------------------------------------
[^_]+ any character except: '_' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \3
----------------------------------------------------------------------
_ '_'
----------------------------------------------------------------------
( group and capture to \4:
----------------------------------------------------------------------
string 'string'
----------------------------------------------------------------------
[^_]+ any character except: '_' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \4
----------------------------------------------------------------------
_ '_'
----------------------------------------------------------------------
( group and capture to \5:
----------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \5
----------------------------------------------------------------------
_ '_'
----------------------------------------------------------------------
( group and capture to \6:
----------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \6
----------------------------------------------------------------------
_ '_'
----------------------------------------------------------------------
( group and capture to \7:
----------------------------------------------------------------------
[^_]+ any character except: '_' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \7
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------