Regex only retrieves the first necessary element but not all of them
Posted
by Serge
on Stack Overflow
See other posts from Stack Overflow
or by Serge
Published on 2010-03-19T11:48:36Z
Indexed on
2010/03/19
11:51 UTC
Read the original article
Hit count: 389
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.
© Stack Overflow or respective owner