Reputation: 65
Imagine that I have this delimiter that is on the end of the string: -holyday[part1]
, I want to match only the -holyday
and ignore if exists [part1]
. A complete string example: with my family - holyday[part1]
.
This is what I have right now but I can only make it match with the [part1]
:
\-\s?([\w\[\]]+)$
Upvotes: 1
Views: 1032
Reputation: 163457
You could use a positive lookahead to assert that your content begins with a -
and what follows is [part1]
Maybe this is what you are looking for:
Explanation
-
\s
[\w\s]+
(?=
\[\w+\]
)
If the pattern for -
and [text]
occurs multiple times and you want only the last occurence, you could for example add another negative lookahead (?!.*\[.*\])
to assert that
no more patterns like [text]
follow. (text like part1
)
-\s[\w\s]+(?=\[\w+\](?!.*\[.*\]))
Upvotes: 0