0

I am trying to parse a C language code in java and I encountered statements like

printf("hello world");

I was using Pattern.compile("printf/(/".*/"/)"); but was getting an error stating that

/( is not a valid escape sequence

Please give a way to tackle this kind of scenario.

1
  • 3
    use backslash to escape Commented Mar 27, 2015 at 13:30

1 Answer 1

2

To escape a character, you need to use backslash instead of forward slash. Since ( is a special meta character, you need to escape that also.

Pattern.compile("printf\\(\".*?\"\\);");

Example:

String value = "printf(\"hello world\");";
System.out.println(value.matches("printf\\(\".*?\"\\);"));
//=> true
Sign up to request clarification or add additional context in comments.

Comments

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.