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?
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?
matches
automatically anchors the regex, so the regex has to match the whole string. 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