# Matching a pattern in many lines of text

This is the relevant piece of my code (false is returned if the whole cycle is finished, pattern is a String passed to the function):

for (FileLine fileLine : fileLines) {
itemText = fileLine.getText();

itemStrings = new ArrayList<String>();
for (String itemString : itemStrings) {
if (itemString.equalsIgnoreCase(pattern)) {
return true;
}
}
}


How can I speed this up? I've tried using regex, something like:

//before the for cycle
Pattern regexPattern = Pattern.compile(pattern);

//in the cycle
if (regexPattern.matcher(itemText).matches())
return true;


But it doesn't work for some reason. Maybe I'm doing it wrong, or there's a completely different way of accomplishing this?

You should probably be using find() instead of matches() (source).

I would recommend using regexpal to test out your pattern, just in case.

Whether or not the Regex solution is faster, you'd have to run some tests, but I suspect it's MUCH faster.

Why are you creating a new ArrayList each time, then filling it with (read: basically copying each element of) a list you created from an array? You have no need right here for the dynamic-ness of a List.

Why not skip the two extraneous creations (and the resulting GC work, if this is a big file), and work with the array directly...and only create the ArrayList once you find a match?

for (FileLine fileLine : fileLines) {
itemText = fileLine.getText();
String[] candidate = itemText.split(" ");

for (String itemString : candidate) {
if (itemString.equalsIgnoreCase(pattern)) {

// Note: if itemStrings is local, you don't even have to create it.
// This code is only necessary if it's an instance or class variable.
itemStrings = new ArrayList<String>();

The biggest difference here is that if you don't find a match, itemStrings never gets set -- whereas with the original code, it'll be set to the last line, regardless of whether that line was a match.