You need to be clear exactly what your boundaries and delimiters are. In the case of your example, I'm guessing the delimiters are the $
at the start of the line which is followed by a "word" (composed of \w
characters) then an =
symbol.
If we make a simplifying assumption and say that a $
at the start of the line is the delimiter (regardless of what follows) then we can do something like this:
(?xms: # Switch on "x" (comment-mode), "m" (Allow "^" to mean start of line rather than start-of-input), "s" (dot-all)
^ \$ # Match $ at start of line only
( [^=]+ ) # Then capture everything until the next '=' (you may want to use \w+ here)
= # Skip the =
(
(?:
(?! \n (?: ^ \$ | \z ) ) # Stop if we reach the \n before the next $ at the start of a line or the \n at the end of the input
. # Otherwise accept any character (including \n thanks to dot-all mode)
) *
) # This will capture everything (excluding the trailing newline) up to the next $ at the start of the line (or all the way to the end of the input)
)
You can then use this in a loop:
static final Pattern pattern = Pattern.compile( " <the above RE> ");
. . .
Matcher matcher = pattern.matcher(myInputString);
while (matcher.find()) {
map.put(matcher.group(1), matcher.group(2));
}
(I haven't tested any of this at all but it should get you started)