I personally needed something like this, but a bit more robust, so here is what I have come up with. I am sure there are some improvements that could be made to error handling and perhaps to the code itself, but hopefully it gets you going in the right direction.
(defun cw_Substring (InputString Delim1 Delim2 Mode TrimMode / Delim1Pos Delim2Pos Results)
;Written by Chris Wade - 06/25/2018
;If you don't want to use Delim2, use nil for the option
;Modes:
; 0.1 - Returns string before Delim1
; 0.2 - Returns string before Delim2
; 1 - Returns String beteen Delim2 and Delim2
; 2.1 - Returns String after Delim1
; 2.2 - Returns String after Delim2
;TrimMode - T will trim spaces out of the results, nil won't
(if Delim1
(setq Delim1Pos (vl-string-search Delim1 InputString))
)
(if Delim2
(setq Delim2Pos (vl-String-search Delim2 InputString (+ Delim1Pos 1)))
)
(cond
((= Mode 0.1);Finds string before Delim1
(setq Results (substr InputString 1 Delim1Pos))
)
((= Mode 0.2);Finds string before Delim2
(setq Results (substr InputString 1 Delim2Pos))
)
((= Mode 1);Finds String between Delim1 and Delim2
(setq Results (substr InputString (+ Delim1Pos 2) (- Delim2Pos (+ Delim1Pos 1))))
)
((= Mode 2.1);Finds string after Delim1
(setq Results (substr InputString (+ Delim1Pos 1)))
)
((= Mode 2.2);Finds string after Delim2
(setq Results (substr InputString (+ Delim2Pos 2)))
)
)
(if TrimMode
(setq Results (vl-string-trim " " Results))
)
Results
)
And here is some code you could use to test it with:
(defun c:DelimTest (/)
(setq H "Begin=Between=End1|End2");Change this string to test
(princ "\nThis is without trimming:")
(princ "\nBefore first =\n")
(princ (cw_substring H "=" "|" 0.1 nil))
(princ "\nBefore |\n")
(princ (cw_substring H "=" "|" 0.2 nil))
(princ "\nBefore Second =\n")
(princ (cw_substring H "=" "=" 0.2 nil))
(princ "\nBetween = and |\n")
(princ (cw_substring H "=" "|" 1 nil))
(princ "\nBetween = and =\n")
(princ (cw_substring H "=" "=" 1 nil))
(princ "\nAfter first =\n")
(princ (cw_substring H "=" "|" 2.1 nil))
(princ "\nAfter |\n")
(princ (cw_substring H "=" "|" 2.2 nil))
(princ "\nAfter second =\n")
(princ (cw_substring H "=" "=" 2.2 nil))
(princ "\nThis is with trimming:")
(princ "\nBefore first =\n")
(princ (cw_substring H "=" "|" 0.1 T))
(princ "\nBefore |\n")
(princ (cw_substring H "=" "|" 0.2 T))
(princ "\nBefore Second =\n")
(princ (cw_substring H "=" "=" 0.2 T))
(princ "\nBetween = and |\n")
(princ (cw_substring H "=" "|" 1 T))
(princ "\nBetween = and =\n")
(princ (cw_substring H "=" "=" 1 T))
(princ "\nAfter first =\n")
(princ (cw_substring H "=" "|" 2.1 T))
(princ "\nAfter |\n")
(princ (cw_substring H "=" "|" 2.2 T))
(princ "\nAfter second =\n")
(princ (cw_substring H "=" "=" 2.2 T))
(princ)
)