Rice
Rice

Reputation: 3531

Java Regular Expression not evaluating

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.

Upvotes: 1

Views: 50

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074495

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

Upvotes: 2

Related Questions