[英]regex to extract numbers from string
I have multiple strings and I want to extract only matching numbers .我有多个字符串,我只想提取匹配的数字。
Sample strings :示例字符串:
abc_efghi_92458_ijk_mno_uvw_test_v2_ghi003
AB_CD_E01_436873_MY_NAME_TESTING_O_001
testing-check-100001-23244-sln-001
I am expecting output as :我期望输出为:
92458
436873
23244
I tried with +([^_]+)
and +([\\d{5,6}]+)
我试过
+([^_]+)
和+([\\d{5,6}]+)
No luck Thanks没有运气谢谢
For your example data you might use a capturing group and a backreference.对于您的示例数据,您可能会使用捕获组和反向引用。
The value is in group 2该值在第 2 组中
.*([-_])(\d+)\1
You can just simply iterable throw each string from the list.您可以简单地迭代从列表中抛出每个字符串。 Then apply this regex:
.*([-_])(\\d+)\\1
and get the group 2 if it matches.然后应用此正则表达式:
.*([-_])(\\d+)\\1
并获取第 2 组(如果匹配)。 Then add the output to the result.然后将输出添加到结果中。
This will give you any contiguous sequence of numbers:这将为您提供任何连续的数字序列:
/[.*!\d](\d+)[.*!\d]/g
let arr = [ "abc_efghi_92458_ijk_mno_uvw_test_v2_ghi003", "AB_CD_E01_436873_MY_NAME_TESTING_O_001", "testing-check-100001-23244-sln-001" ] let re = /[.*!\\d](\\d+)[.*!\\d]/g let res = arr.map(str => str.match(re)) console.log(res)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.