简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM