Can anybody help me with retrieving some elements from the following example text:
sdfaasdflj asdfjl;a
AB-12/34 BC-/85 CD-//8 DD-77
DE-78/9
EE-78-98
asdf; asdjf
It is necessary to get the following elements: AB-12/34, BC-/85, CD-//8, DD-77, DE-78/9
When I'm using a regular expression like this:
\s*(?<elements>\b[A-Z]{2}-[/0-9]+\b)
everything works fine - all the necessary elements are being retrieved (except for the EE element are amonth them, but it doesn't matter). The problem is that this line is a part of a more complex regex, so when I'm trying to apply a regex like this:
(?s).*\sas.*?
\s*(?<elements>\b[A-Z]{2}-[/0-9]+\b)*.*
.*as
开发者_如何学运维
It only returns me just the first AB-12/34 element and nothing else. How to correct the regex to get all the elements? TIA.
To match the block "(?<elements>\b[A-Z]{2}-[/0-9]+\b)*"
multiple times in your example, you need to include the whitespace in it. I.e.:
"(?<elements>\s*\b[A-Z]{2}-[/0-9]+\b)*"
If you do not want to capture it, try ""(?:\s*(?<elements>\b[A-Z]{2}-[/0-9]+\b))*"
. I am not sure how the named capture group, inside a non-capturing group will work, though. =)
精彩评论