Reputation: 6835
I have a text file that I iterate over and want to check for multiple substrings in each line (1 substring will exist per line).
my regex is the following
String rE = "(AGG|TIP|IDV|DVY|IYR|LQD|HYG|EMB|ACWI|ACWX|EFA|SCZ|EEM|IWB|IWF|IWD|IWM|IWO|IWN|IWV|IVV|IVW|IVE|IJH|IJK|IJJ|MUB|IJR|IJS|IJT|SPY)"
and a line of my text file looks like this:
SPY,6696832,31080,140.7,400,140.69,140.69,6396960,299872
yet when i do:
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
System.out.println("Starting");
while ((retStr = in.readLine()) != null) {
if(retStr.matches(tickers)){
System.out.println(retStr);
}
}
I do not find my strings.
The code compiles and runs perfectly. I iterate over the file, yet I never find my result.
Could I please have some help on what I am doing wrong?
Upvotes: 2
Views: 6590
Reputation: 27793
For better performance you should compile the regular expression. The matches
method on String recompiles the expression each time, it is not meant to be used inside a loop.
Here's an example
import static org.junit.Assert.assertEquals;
import java.util.regex.Pattern;
import org.junit.Test;
public class Example {
@Test
public void shouldMatchString() {
Pattern p = Pattern.compile("^(AAA|BBB|CCC)");
assertEquals(true, p.matcher("AAA,1,2,3,4,5").find());
assertEquals(false, p.matcher(" AAA").find());
}
}
Find does not match against the whole string, so I'm using ^
to match the begin of the input.
Upvotes: 1
Reputation: 129507
Just add .*
to the end of your regex (.*
matches anything):
String rE = "(AGG|TIP|IDV|DVY|IYR|LQD|HYG|EMB|ACWI|ACWX|EFA|SCZ|EEM|" +
"IWB|IWF|IWD|IWM|IWO|IWN|IWV|IVV|IVW|IVE|IJH|IJK|IJJ|MUB|IJR|IJS|IJT|SPY).*"
Upvotes: 4