I have a use case that requires the identification of many different pieces of text between any two characters.
For example,
- String between a single space and
(
:def test()
would returntest
- String between a word and space (
paste
), and a special character (/
):@paste "game_01/01"
would return"game_01
- String between a single space and
(
with multiple target strings:} def test2() { Hello(x, 1)
would returntest2
andHello
To do this, I'm attempting to write something generic that will identify the shortest string between any two characters.
My current approach is (from chrisz):
pattern = '{0}(.*?){1}'.format(re.escape(separator_1), re.escape(separator_2))
And for the first use case, separator_1 = \s
and separator_2 = (
. This isn't working so evidently I am missing something but am not sure what.
tl;dr How can I write a generic regex to parse the shortest string between any two characters?
- Note: I know there are many examples of this but they seem quite specific and I'm looking for a general solution if possible.