This should work, it captures unique values only once:
(\d+)(?:_)(?![\s\S]*\1)
Try Demo here
Explanation
(\d+)(?:_)(?![\s\S]*\1)
1st Capturing Group (\d+)
\d+
matches a digit (equal to [0-9]
)
- Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
Non-capturing group (?:_)
_
matches the character _
literally (case sensitive)
Negative Lookahead (?![\s\S]*\1)
Assert that the Regex below does not match
Match a single character present in the list below [\s\S]*
- Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\s
matches any whitespace character (equal to [\r\n\t\f\v ]
)
\S
matches any non-whitespace character (equal to [^\r\n\t\f\v ]
)
\1
matches the same text as most recently matched by the 1st capturing group