1

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;                                            \n"
        +"uniform mat4 modelViewMatrix;                                         \n"
        +"uniform mat4 projectionMatrix;                                        \n"
        +"void main()                                                           \n"
        +"{                                                                     \n"
        +"      gl_Position = projectionMatrix * modelViewMatrix * a_position;  \n"
        +"}                                                                     \n";

Thanks a lot!

1 Answer 1

2

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) .

Sign up to request clarification or add additional context in comments.

1 Comment

Kilian is correct, can't paste my unit test in here, but just change your regex line to String attributes = VS.replaceAll("(?m)^(?!attribute).*$", ""); as Kilian said

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.