开发者

Regex only retrieves the first necessary element but not all of them

开发者 https://www.devze.com 2022-12-23 17:09 出处:网络
Can anybody help me with retrieving some elements from the following example text: sdfaasdflj asdfjl;a

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. =)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号