The syntax for extracting the first occurrence of a substring to end of string is well known:
Code: Select all
set "tail=%str:*x=%"
And there’s a kludgy way to get the start of a string up to the first occurrence of the substring:
Code: Select all
set "head=%str:x=" & rem."%"
I was messing around with the latter when I stumbled across the following:
Code: Select all
@echo off & setlocal enableextensions disabledelayedexpansion
set "x=monotonous"
set "x1=%x:o=" & set "x2=%"
set x
endlocal & goto :eof
Var x1 contains: m, and x2 ends up with: us. From the last occurrence of the substring to the end of string, in other words.
All the usual caveats apply, of course. The substring is case-insensitive, but the replacement string isn’t. Quotes must be doubled. Per cents, tildes, asterisks and equal signs must be encoded. And it only works with %-variables.
But there’s more. Run my little snippet again with echo on. The x2 var is set four times, each time with the contents of the substring between the previous occurrence of the letter o and the next one.
BFN!
- SB