1

I have a string that is changes frequently, in the form of :

*** START OF THIS PROJECT FILENAME ***

Where FILENAME can be multiple words in different instances. I tried running the regex :

Pattern.matches("\\*\\*\\* START OF THIS PROJECT ", line);

where line is equal to one such strings. I also tried using the Matcher, where beginningOfFilePatter is also set to the same regex pattern above:

Matcher beginFileAccelerator;
beginFileAccelerator = beginningOfFilePattern.matcher(line);
if (beginFileAccelerator.find() 
//Do Something

Ive exhuastively tried at least 30 different combinations of regex, and I simply can't find the solution. If anyone could lend me an eye I would greatly appreciate it.

1 Answer 1

2

Pattern.matches tries to match the entire string against the pattern, because under the covers it uses Matcher#matches, which says:

Attempts to match the entire region against the pattern.

In your case, that will fail at the end, because the input doesn't end with "PROJECT ". It has more after that.

To allow anything at the end, add .*:

Pattern.matches("\\*\\*\\* START OF THIS PROJECT .*", line)
// Here -----------------------------------------^

Live Example

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.