c# - C# Regex for getting all possible matches
问题描述
I'd like to extract all the occurences of this regex
\d{7,8}
(every number that has 7 or 8 length)
The input cuould be something like
asd123456789bbaasd
what I want is an array with:
["1234567", "12345678", "2345678", "23456789"]
all the possible occurencies of a number that has 7 or 8 lenth.
Regex.Matches works diferent, it returns all the consecutive occurencies of matches.... ["12345678"]
Any idea?
解决方案
对于重叠匹配,您需要在前瞻中捕获。
(?=(\d{7}))(?=(\d{8})?)
因此,如果有 7 位匹配,它们将在组(1)中,如果 8 位匹配,则在组(2)中。在 .NET Regex 中,您可能可以为两个组使用一个名称。
要仅在前面有 8 个时才获得 7 位匹配,请在此演示中?
删除after 。(\d{8})
推荐阅读
- r - 如何通过ggplot geom_bar中的组填充
- neural-network - 哪些层更不能容忍神经网络中的错误?
- jquery - 循环通过图像“srcset”属性并替换文件类型后缀
- javascript - 如何将自定义 DuckDuckGo 搜索定向到 html 中的特定站点?
- php - 如何修复“重定向你太多次”。
- c - 默认安装的 Code::Blocks 在程序结束时不会因“按任意键继续”而暂停
- c - “UA_ReadRequest_deleteMembers”错误问题的 OPC-UA 函数
- python - 从python检测macos中的暗模式
- xamarin - Cocossharp 精灵掩蔽
- python - 如何在嵌套字典中查找重复项或重复项?