dentix
dentix

Reputation: 11

Regular Expression to get all characters before "_" and after "-"

I want to get "FileType" using a Regular Expression.

Here is a example string:

randomname-id-mininthostname-FileType_20221118.zip

I tried (?=-).*(?<=_) but it will return "-minint-FileType_"

Upvotes: 1

Views: 41

Answers (1)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112682

Replace .* which stands for any number of any character by [^-]* which stands for any number of any character except -. Or even better, since we want to capture at least one characxter: [^-]+.

You inverted lookahead and lookbehind. I corrected that as well.

(?<=-)[^-]+(?=_)

Upvotes: 2

Related Questions