Reputation: 72510
I have some lines in a text file like this:
==Text==
I'm trying to match the start, using this:
line.matches("^==[^=]")
However, this returns false for every line... little help?
Upvotes: 1
Views: 320
Reputation: 111265
You can also use String.startsWith("==");
if it is something simple.
Upvotes: 1
Reputation: 10880
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
Upvotes: 0
Reputation: 4143
.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("==[^=]*==")
Upvotes: 0
Reputation: 370102
matches
automatically anchors the regex, so the regex has to match the whole string. Try:
line.matches("==[^=].*")
Upvotes: 4