a "sample" text "fragment".
The initial attempt is something like this:
But since regular expressions use greedy matching, it will match:
"sample" text "fragment"
The fix for this would be to change any character (.) to match any character except " ([^"]):
And this solution works, but what if the delimiter is a string and not just a single character? For example:
a <!--sample--> text <!--fragment-->.
You cannot use any character except, you need a any character except one that is followed by, and this is called negative lookahead.
To match any character except one that is followed by --> you can use the following expression:
Applying this on the sample text matches all text except the e and t in sample and fragment respectively. If we want the entire inner text matched, we need to add the excluded character:
This will match the entire text, so now we can apply the delimiters:
Derek Slager has an online regular expression tester.