Extracting lines containing a word from a Java string using regex
I have a problem with the following code. I want to extract the lines containing the word attribute. I've used http://www.regextester.com/ where this seems to work just fine, but in my program the string remains intact. I don't get why. Could it be that it doesn't use the newline characters correctly, so the ^ and $ get the entire string?
Here's the code:
String attributes = VS.replaceAll("^(?!attribute).*$", "");
And here's the string:
String vShaderStr =
"attribute vec4 a_position; \n"
+"attribute vec3 a_normal; \n"
+"attribute vec2 a_texCoord; 开发者_StackOverflow \n"
+"uniform mat4 modelViewMatrix; \n"
+"uniform mat4 projectionMatrix; \n"
+"void main() \n"
+"{ \n"
+" gl_Position = projectionMatrix * modelViewMatrix * a_position; \n"
+"} \n";
Thanks a lot!
Yes, ^
and $
match only the start and end of a String, not of a \n
-terminated line within a String
. You need to compile your regex with the flag MULTILINE
to change that. So the solution is to start your regex specification with (?m)
.
精彩评论