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

Time:10-13

I have following regular expression in my validator :

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

So as you see here : [\\p{L}\\ \\'\\-\\,\\.] [\\p{L}//]

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?

CodePudding user response:

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.

  • Related