Reputation: 6561
I am trying to build a "super non-greedy" regex, for lack of a better phrase. The problem I'm having is distilled as follows:
https://regex101.com/r/wuwOGd/2
Regex: \/\*\*(.*?)\*\/\w+\d+
Sample String: /**word1*/asdf /**word2*/abc123
What I want it to do: Only match the second token so I can extract word2
.
What it's doing: Matching word1*/asdf /**word2
which is technically correct, so I can't blame the regex for doing what I told it to do. But is there a way I can have the regex "fail" as soon as it has to expand beyond the first */
?
I'm using python in this particular case to match comment blocks attached to functions with certain signatures.
Edit:
As pointed out below, it turns out the magic word I was searching for was "tempered", not "super"!
Upvotes: 0
Views: 82
Reputation: 22837
/\*{2}((?:(?!\*/).)*)\*/\w+\d+
Alternatively, without having to capture it (assuming PCRE). See regex in use here
/\*{2}\K(?:(?!\*/).)*(?=\*/\w+\d+)
This method uses a tempered greedy token to ensure it matches any character except where */
is found.
Upvotes: 2
Reputation: 23347
You can use negated class instead of non-greedy repetition:
\/\*\*([^*]*)\*\/\w+\d+
https://regex101.com/r/wuwOGd/3
as the token you look for is delimited with *
it's quite safe.
Upvotes: 2