What would be the shortest solution to capture a string between two markers?
For example, let’s imagine I have
blahblahblah —foobar– blahblahblah
And I want to capture “foobar”.
I can capture the string “—foobar–” quite easily with a regex.
But since I don’t want the prefix “—” and suffix “–“, I need many steps to clean up the result :
- get position of prefix “—” (since we already captured the bloc, we know the position is 0)
- get length of “—” (for the sake of the example, let’s assume we don’t know the size of the limiter, it can be “—” or “–” or “-“)
- get position of suffix “–“, starting at position position_prefix+prefix_length
- substring between position_prefix+prefix_length and position_suffix
Of course I can build myself such a function to do that if I need it, but is there already a built-in solution for that?
Something like captureWithoutDelimiters(group_regex, prefix_regex, suffix_regex) ?
How about:
Then you can just print out what’s caught by the first capture group.
Explanation: