tags:

views:

139

answers:

7

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?

+7  A: 

As I remember, method matches() searches for exact match only.

Roman
An exact and complete match.
Daniel Straight
This is true, and the documentation could be a lot clearer about it -- http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html
mobrule
+5  A: 

matches automatically anchors the regex, so the regex has to match the whole string. Try:

line.matches("==[^=].*")
sepp2k
A: 
try line.matches("^==[^=]*==$")
Schildmeijer
Fixed it for ya.
Paul Tomblin
A: 

Try with line.matches("^==(.+?)==\\s*$")

splix
A: 

.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("==[^=]*==")

TheJacobTaylor
This should produce a match of == followed by arbitrary non-equals characters, followed by another ==.
TheJacobTaylor
Bummer, minor edits moved my answer to look newer than it is.
TheJacobTaylor
A: 

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

Il-Bhima
+1  A: 

You can also use String.startsWith("=="); if it is something simple.

serg