Your content contains no "
quotes, and no text gm
, so why would you expect that regex to match?
FYI: Syntaxes like "foo"gm
or /foo/gm
are something other languages do for regex literals. Java doesn’t do that.
The g
flag is implied by the fact that you’re using a find()
loop, and m
is the MULTILINE
flag that affects ^
and $
and you can specify that using the (?m)
pattern, or by adding a second parameter to compile()
, i.e. one of these ways:
Pattern p = Pattern.compile("foo", Pattern.MULTILINE);
Pattern p = Pattern.compile("(?m)foo");
Your regex should simply be:
(?m)^.*(?==)
which means: Match everything from the beginning of a line up to the last =
sign on the line.
Test
String content = "This is an example.=This is an example\nThis is second:=This is second";
String regex = "(?m)^.*(?==)";
Matcher m = Pattern.compile(regex).matcher(content);
List<String> mKeys = new ArrayList<>();
while (m.find()) {
mKeys.add(m.group());
}
System.out.println(mKeys);
Output
[This is an example., This is second:]
solved No match for Java Regular Expression