2

How shall we write a regular expression to validate a string that should have a length of a minimum of 1 character and a maximum of 50 characters, have both upper case and lower case, alphanumeric, include space, and have mostly used special characters like @,._-&$#? The first character should be either alphabet or number then the rest can be as mentioned above.

*If it is only one character then it should be an alphanumeric one

I have tried a regex with my limited knowledge which looks like

^[a-zA-z]*[a-zA-Z\d\-_@&$%#\s]{1,50}$

But I am not able to match the string if there is only one character given, can anyone guide me to fix this

First Arachne
  • 786
  • 1
  • 8
  • 18
Christine Shaji
  • 157
  • 1
  • 2
  • 10
  • Can include one space or more? – alextrastero Oct 27 '20 at 07:19
  • Can Include 1 or more spaces but the begining of the text should be an alphabet @alextrastero – Christine Shaji Oct 27 '20 at 12:44
  • Thanks for the reference, I will check that as well @WiktorStribiżew – Christine Shaji Oct 27 '20 at 12:45
  • Also, see https://stackoverflow.com/questions/11511154 and https://stackoverflow.com/questions/19605150. – Wiktor Stribiżew Oct 27 '20 at 12:47
  • 1
    @WiktorStribiżew I have tried to create a regex with my limited knowledge and your reference and i have edited the question with that. can you help me fix this – Christine Shaji Oct 27 '20 at 15:25
  • Try `^[a-zA-Z][\w@,.&$%#\s-]{0,49}$`. Or `^(?=[a-zA-Z])[\w@,.&$%#\s-]{1,50}$` – Wiktor Stribiżew Oct 27 '20 at 15:34
  • its working..Thanks a lot @WiktorStribiżew .. I had one more question once the language is changed for this from english and the input is coming in some other language, like lets say swedish , then this regex will fail right. If yes, do we any way to fix that? – Christine Shaji Oct 27 '20 at 15:40
  • 1
    Aha, so you want to use a Unicode-aware `\w`? See [this answer](https://stackoverflow.com/a/62772689/3832970), and you will need to use `/^(?=\p{Alphabetic})[\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}\p{Join_Control}@,.&$%#\s-]{1,50}$/u` – Wiktor Stribiżew Oct 27 '20 at 15:43
  • So, what do you need? – Wiktor Stribiżew Oct 27 '20 at 16:59
  • Need to know a method which will validate and pass the string even in case of internationalization. the above reference was confusing me pretty much, what do you suggest me to do @WiktorStribiżew – Christine Shaji Oct 27 '20 at 17:02
  • Either learn regex or pass this task to a colleague. – Wiktor Stribiżew Oct 27 '20 at 17:05
  • Not willing to give up.. will learn about it more and will make it work..Thanks for your time and help..Really appreciated @WiktorStribiżew – Christine Shaji Oct 27 '20 at 17:06
  • got a regex for it `^(?=[\p{​​​​​L}​​​​​0-9])[\w@,.&$%#\s-]{​​​​​1,50}​​​​​$` was doing the job..even `\w` instead of `\p{L}` can be used. but in my case the beginning letter doesnt need to be a unicode digit so used `[\p{L}]` Thanks for giving me the reference and teaching me @WiktorStribiżew – Christine Shaji Oct 27 '20 at 17:25

1 Answers1

2

You can use

/^(?=[\p{L}0-9])[\p{L}\p{N}_@,.&$%#\s-]{1,50}$/u

See the regex demo

Details

  • ^ - start of string
  • (?=[\p{L}0-9]) - the first char must be a Unicode letter (\p{L}) or an ASCII digit
  • [\p{L}\p{N}_@,.&$%#\s-]{1,50} - one to fifty
    • \p{L} - any Unicode letter
    • \p{N} - any Unicode digit
    • _@,.&$%#- - any of these chars
    • \s - any whitespace
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563