Xon
Xon

Reputation: 69

How to make sure a string has a certain format

currently I have a code like this

public class Department {

    public static final String MESSAGE_DEPARTMENT_CONSTRAINTS =
            "Department names should only contain alphanumeric characters and spaces, and it should not be blank\n"
            + "Department names should start with a name, followed by 'Management'";


    public static final String DEPARTMENT_VALIDATION_REGEX = "[\\p{Alnum}][\\p{Alnum} ]*";

    public final String fullDepartment;

    public Department(String department) {
        requireNonNull(department);
        checkArgument(isValidDepartment(department), MESSAGE_DEPARTMENT_CONSTRAINTS);
        fullDepartment = department;
    }

    /**
     * Returns true if a given string is a valid department name.
     */
    public static boolean isValidDepartment(String test) {
        return (test.matches(DEPARTMENT_VALIDATION_REGEX) && (test.indexOf("Management") >= 0));
    }


    @Override
    public String toString() {
        return fullDepartment;
    }

    @Override
    public boolean equals(Object other) {
        return other == this // short circuit if same object
                || (other instanceof Department // instanceof handles nulls
                && fullDepartment.equals(((Department) other).fullDepartment)); // state check
    }

    @Override
    public int hashCode() {
        return fullDepartment.hashCode();
    }

}

I would like the code to only allow only valid departments name to be created

Example:

However, now I'm facing a problem where the word Management can be placed at anywhere and it's still considered valid

Example:

How can I ensure that the word Management is a requirement at the back of a department name when I'm creating a department? Thanks.

Upvotes: 2

Views: 89

Answers (2)

flyingfox
flyingfox

Reputation: 13506

Two ways to do it

a. Using startsWith() and endsWith() in StringUtils ,or just startsWith() and endsWith() that String provides

boolean endsWith = StringUtils.endsWith("Managemet") && !StringUtils.startsWith("Managemet");

b. Using regex .*?Management$,in this expression using .*? to include space and other special characters

String str ="Test Management";
String regex = ".*?Management$";
System.out.println(str.matches(regex));

Upvotes: 2

jbx
jbx

Reputation: 22128

Just change this function to this:

public static boolean isValidDepartment(String test) {
  return test.matches(DEPARTMENT_VALIDATION_REGEX) 
              && test.endsWith("Management") 
              && !test.equals("Management");
}

If you think you will need more sophisticated checks you can also change your department validation regex to:

public static final String DEPARTMENT_VALIDATION_REGEX = "(\\p{Alnum}+ )+Management";

public static boolean isValidDepartment(String test) {
  return test.matches(DEPARTMENT_VALIDATION_REGEX);
}

Note that this will still allow "Management Management" and also "M8n8g3m3nt Management" since you used \\p{Alnum}. If you only need alphabet characters use \\p{Alpha}. If you want to catch the exception of "Management Management" you might want to do:

public static boolean isValidDepartment(String test) {
  return test.matches(DEPARTMENT_VALIDATION_REGEX) 
              && !test.equals("Management Management");
}

You should be able to do it all through the regex, but might get too complicated and unreadable for just one exception you can easily check with .equals().

Upvotes: 2

Related Questions