I have the regex: (?ms)(?<attribute>\[.+?\]|public|private|\s)+?class
and I have the text:
[attribute]
public int a;
[attribute C]
[attribute B]
public class Test{
}
I would like to know why the regex that I posted matches:
[attribute]
public int a;
[attribute C]
[attribute B]
public class
I think it should match:
[attribute C]
[attribute B]
public class
Correct me if I am wrong. I think the way that the regex is supposed to be read is:
Find either an attribute ( [ some attribute ] ) or public key word or private keyword or space.
So first the regex engine should match [attribute], then the ‘\n’ (new line), then the public keyword. After these, the keyword int is not an option, so why does it match it?
The problem is that you are using a dot which matches anything, including close square brackets, whitespace, and (in single-line mode) newlines:
You should use this instead:
Explanation: