I have some lines in a text file like this:
==Text==
I'm trying to match the start, using this:
line.matches("^==[^=]")
How开发者_如何学JAVAever, this returns false for every line... little help?
As I remember, method matches()
searches for exact match only.
matches
automatically anchors the regex, so the regex has to match the whole string. Try:
line.matches("==[^=].*")
You can also use String.startsWith("==");
if it is something simple.
Try with line.matches("^==(.+?)==\\s*$")
try line.matches("^==[^=]*==$")
.matches only returns true if the entire line matches. In your case, the line would have to start with '==' and contain exactly one character that was not equals. If you are looking to match that string for the whole line:
line.matches("==[^=]*==")
If I remember correctly, matches
will only return true if the entire line matches the regex. In your case it won't. To use matches
you will need to extend your regex (using wildcards) to match to the end of the line. Alternatively you could just use Matcher.find()
method to match substrings of the line
精彩评论