1

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.

Nenad Bulatović
  • 7,238
  • 14
  • 83
  • 113
  • Possible duplicate of [How to check whether a string contains at least one alphabet in java?](https://stackoverflow.com/questions/14278170/how-to-check-whether-a-string-contains-at-least-one-alphabet-in-java) – Tom Sep 11 '17 at 13:50
  • 2
    Do you want at least 1 character, or 1 letter ? Thats very different. – Asew Sep 11 '17 at 13:50
  • 2
    If you don't allow spaces in beginning or end, wouldn't it be more user-friently to `trim()` string before saving it? Spaces aren't usually visible, this will appear confusing and frustrating to users. – M. Prokhorov Sep 11 '17 at 13:52
  • Is `"A"` a valid input? – anubhava Sep 11 '17 at 13:58
  • @Asew one letter, could be letter not used specifically in English alphabet (a-z,A-Z) – Nenad Bulatović Sep 11 '17 at 14:01
  • @M.Prokhorov valid point, but I leave that to front end developers, mine is to take care of back end, if they haven't already trimmed in JavaScript. – Nenad Bulatović Sep 11 '17 at 14:02
  • @anubhava yes, single letter is allowed – Nenad Bulatović Sep 11 '17 at 14:03
  • 1
    `trim()` can be used in front-end or back-end. Back-end development would be better for string formatting since the user can't turn it off. You should definitely sanitize your strings on the back-end and not rely on anything front-end based. – ctwheels Sep 11 '17 at 14:04

3 Answers3

2

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
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

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

ctwheels
  • 21,901
  • 9
  • 42
  • 77
  • 1
    @NenadBulatovic Apparently java doesn't work nice with negative lookbehinds. I've edited my regex to use a negative lookahead instead of a negative lookbehind to ensure the string doesn't end with hyphens or spaces – ctwheels Sep 11 '17 at 14:27
0

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

yılmaz
  • 1,818
  • 13
  • 15