2

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?

3
  • matches(...) must match the whole String. Consider showing more file lines so we can see other configurations of your text lines that you must analyze. Commented Nov 25, 2012 at 2:44
  • it says in the documentation that it takes a regex as an argument: docs.oracle.com/javase/1.5.0/docs/api/java/lang/… Commented Nov 25, 2012 at 2:47
  • jason: I don't see any posts or comments that disagree with that statement. Commented Nov 25, 2012 at 2:48

2 Answers 2

4

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).*"
Sign up to request clarification or add additional context in comments.

4 Comments

so close and yet so far! thanks so much. will accept when i can!!
@jasonm: he guessed correctly that all your lines of text began with the String your seek. Next time please don't make us guess and show more lines of the text file.
regardless its obvious.. "*.(FOO|BAR).*"
@jasonm, Did he guess correctly? Does the match always occur at the beginning of the line? If so, you should not add .* to the front of the regex. (I'm assuming that's what you meant to say in your comment; *.(FOO|BAR).* won't even compile.)
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.