0

My program is used to filter out names starting with a capital letter [A-M] and to filter out any names with a length less than 5 and greater than 9. The code does filter out the names with a length less than 5, but when I input a name with a length greater than 9, it just cuts off the rest of the name.

Ex: Bartholomew would cut off to Bartholom rather than just not using Bartholomew.

I have tried to move the length flag to different spots in the regex field. Other than that, I do not know regex well enough to try much more. As for putting these strings into another function just to test the lengths - I am trying to make it in one regex field.

import java.io.File;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Egor {
    public static void main(String args[]) throws Exception{
        Scanner s = new Scanner(new File("Names.dat"));
        String[] names = new String[30];

        int i = 0;
        while(s.hasNext()) {
            names[i] = s.next();
            i++;
        }
        String store = "";
        for (String str: names) {
            store = store + str + " ";
        }
        System.out.println(store);

        Pattern checkName = Pattern.compile("([A-M][a-z]{5,9})");

        Matcher matcher = checkName.matcher(store);
        System.out.println("-----------------------");
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}

The expected should print out names like - Ashley, Brendan, Henry would print out The unexpected is that names like - Bartholomew, with lengths greater than 9 print out to Bartholom

B Noonen
  • 23
  • 4

1 Answers1

-1

You need to add a positive look behind and positive look ahead and positive look behind for the desired characters that separate your names. Based on your code it looks like that would be a start of string anchor or space, and a end of string anchor or space for the look behind and look ahead respectively. Will look something like this:

(?<=\b)([A-M][a-z]{5,9})(?=\b)

Look ahead and look behinds in regex match what is ahead of and behind, but do not include it in the matched result.

Zack
  • 301
  • 1
  • 11