As others have suggested, this is not a suitable task for regex; you
should use a parser.
That being said, if you must use regex and you only care about matching a for loop in the exact format you provided, you may replace the \{ . \}
part with something like \{[\S\s]+\}
in order to match the body successfully.
Here's the full pattern:
for\s*\((\w*\s+\w+\s*\=\s*\d+\s*)?\;(\s*\w+\s*\W+\d+\s*)?\;(\s*\w+\W+)?\)\s*\{[\S\s]+?\}
Regex101 demo.
I made some additional changes to it. For example, allowed more whitespaces where appropriate and made the 2nd and 3rd groups optional, since each of the initialization, the condition and the update (increment/decrement) are indeed optional in Java and to match an infinite loop as well. Feel free to keep or disregard these changes based on your requirements.
However, as I said above, this focuses only on the format of the for loop provided in the question and it will not match every possible way to write a for loop as I previously mentioned in the comments. Some examples of those cases:
// The update/increment may use a different operator.
for (int i = 1; i <= 10; i += 1) { }
// The condition can be any expression that evaluates to boolean.
for (int i = 1; i * 2 <= 10; i++) { }
// A for loop can have no braces.
for (int i = 1; i <= 10; i++)
System.out.println(i);
// Etc.