Nenad Bulatović
Nenad Bulatović

Reputation: 7444

Java regular expression to check if there is at least one letter

I've found lot of variations on this subject on both SO and web, but most (if not all) ask for at least one letter and one digit. I need to have at least one letter. I've tried but I haven't make it right, what I need is that String contain only letters, letters + numbers (any order), dashes and spaces are allowed but not at the beginning or the end of the string. Here is how it looks like right now:

protected static final String PATTERN = "[\u00C0-\u017Fa-zA-Z0-9']+([- ][\u00C0-\u017Fa-zA-Z0-9']+)*";

    public static void main(String[] args) {

        String name;

        //name = "Street"; // allowed
        //name = "Some-Street"; // allowed
        //name = "Street "; // not allowed
        //name = " Street"; // not allowed
        //name = "Street-"; // not allowed
        //name = "-Street"; // not allowed
        //name = "Street"; // allowed
        //name = "1 st street"; // allowed
        //name = "street 5"; // allowed
        name = "111"; // NOT allowed

        if (!Pattern.matches(PATTERN, name)) {
            System.out.println("ERROR!");
        } else System.out.println("OK!");
    }
}

How do I add check if there is at least one character?

No matter if it is at the beginning or end, or if there is space or dash between it and numbers. There just have to be at least one character.

Upvotes: 1

Views: 3133

Answers (3)

yılmaz
yılmaz

Reputation: 1826

Following regex does the job:

(?=.*[[:alpha:]])[[:alnum:]]{1}[[:alnum:] -]*[[:alnum:]]{1}
  • (?=.*[[:alpha:]]) part guarantees that alpha character [A-Za-z] exists inside word.

  • [[:alnum:]]{1} part guarantees that string starts with alphanumeric character [A-Za-z0-9]

  • [[:alnum:] -]* alphanumeric characters, space and dash characher might exist here.

  • [[:alnum:]]{1} part guarantees that string ends with alphanumeric character [A-Za-z0-9]

To see it live https://regex101.com/r/V0lesF/1

Upvotes: 0

ctwheels
ctwheels

Reputation: 22817

If I understand correctly, and according to what you've presented, you have the following conditions:

  • At least 1 letter
  • Can contain digits (but only if the previous condition is met)
  • Dashes and spaces are allowed only if they are not at the beginning or end of the string

Based on these conditions, the following regex will work:

^(?![ -]|\d+$)[[:alnum:] -]+(?<![ -])$

To see this regex in use, click this link.
This regex works as follows:

  • Ensure the string doesn't begin with hyphen - or space
  • Ensure the string isn't composed of only digits
  • Ensure the string contains between one and unlimited alphanumeric characters
  • Ensure the string doesn't end with hyphen - or space

This will give you the following matches

Street
Some-Street
Street
1 st street
street 5

The regex will fail to match the following strings (as per your examples)

Street 
 Street
Street-
-Street
111

Edit

Negative lookbehinds can sometimes cause issues in certain languages (like java).

Below is an adapted version of my previous regex that uses a negative lookahead instead of a negative lookbehind to ensure that the string doesn't end with hyphen - or space .

^(?![ -]|\d+$)(?:(?![ -]$)[\pL\pN -])+$

You can see this regex in use here

Upvotes: 1

anubhava
anubhava

Reputation: 785156

You can use this regex for your problem:

^(?=.*\pL)[\pL\pN]+(?:[ -]+[\pL\pN]+)*$

RegEx Demo

For Java use:

final String regex = "^(?=.*\\pL)[\\pL\\pN]+(?:[ -]+[\\pL\\pN]+)*$";

RegEx Breakup:

  • ^: Start
  • (?=.*\pL): Using a lookahead make sure we have at least one unicode letter somewhere
  • [\pL\pN]+: Match one or more unicode letter or unicode digit
  • (?:: Non-capturing group start
    • [ -]+: Match one or more space or hyphen
    • [\pL\pN]+: Match one or more unicode letter or unicode digit
  • )*: Non-capturing group end. * means zero or more of this group.
  • $: End

Upvotes: 2

Related Questions