Repeating named capture groups
I have a string with a field like t开发者_开发问答his: id="ID-120-1, ID-141-5, ID-92-5, N/A"
I'd like to capture only the "ID"s to a named capture group (i.e. without the "N/A" or other items that might creep in). I thought this might work, but no luck:
\bid=\"(?<id>(ID-\d+-\d+)+)
Any ideas?
The expression you are using only returns one because you are counting on the start of the id to be present in front of each ID value. The following adjustment should fix that.
(?:(?:=\")|(?:,\s))(?<id>(?:ID-\d+-\d+)*)
Another option would be to just drop the id=" check part all together
(?<id>(?:ID-\d+-\d+))
Or you could add the ", " check on to the end of the id to make sure you are in attribute.
(?<id>(?:ID-\d+-\d+))(?:(?:,\s)|(?:"))
You would need to capture commas and spaces also, as they are repeated in your string:
\bid=\"(?<id>(ID-\d+-\d+, )+)
I believe what you are trying to do is not possible with pure regex, especially if IDs and 'N/A' can be intermixed. You will need to have a loop in your program, or if you use Perl or PHP, you can run code in the replacement part of the regex (/e
switch) to add the matches to an array.
精彩评论