Allow space at the end of p{L} in Regex

I have following regular expression in my validator :

validator.addValidation(userNameLayout, "[\\p{L}\\ \\'\\-\\,\\.]+[\\p{L}//]", context.resources.getString(R.string.verification_userdata_name_invalid))

Is there any solution to allow just whitespace at the end of p{L} and nothing else?

So it is possible to enter : ali rezaei

but it is not possible to enter : ali.

Now I have added this +[\S] to end of regex :

"[\\p{L}\\ \\'\\-\\,\\.]+[\\p{L}//]+[\\s]"

It allows a single space at the end but not multiple spaces. What is the solution?

1 answer

  • answered 2021-10-12 17:02 Wiktor Stribiżew

    The following regex:

    "[-\\p{L}\\s',.]*\\p{L}\\s*"
    

    used in validator.addValidation(), should match the string that fully matches

    • [-\p{L} ',.]* - any zero or more letters, -, whitespaces, apostrophes, commas, dots
    • \p{L} - a Unicode letter
    • \s* - zero or more whitespace chars (optional trailing whitespaces).

    See the regex demo.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum