-1

I have a regex "/^[a-zA-Z]{2,50}$/" which i use for an input field. This works perfectly for the English alphabet. But this doesn't work for Foreign alphabets. When I enter any foreign language, for example, Ağustos, the regex fails. How can I solve this?

I use preg_match for testing.

Mujahid Bhoraniya
  • 1,518
  • 10
  • 22
melvin
  • 2,571
  • 1
  • 14
  • 37

1 Answers1

0

The regex to matche any kind of letter from any language is \p{L} so in your case the regex would be \p{L}{2,50}

You can test this on regex101 to see it work

Karan Shishoo
  • 2,402
  • 2
  • 17
  • 32
  • 1
    I have tested this and is not returning match group – melvin Feb 26 '20 at 05:06
  • @melvin then you are probably implementing it incorrectly, you can test it on regex101.com for php and it marks `Ağustos` as a match. I would share a link but regex101 seems to be unable to generate any shareable links right now – Karan Shishoo Feb 26 '20 at 05:08
  • Are you sure? regex101.com is not available now. I tested on other sites and was not getting any result – melvin Feb 26 '20 at 05:09
  • @melvin yes I tested it as i was posting the answer, it is loading a lot slower than normal and not generating shareable links but it is working – Karan Shishoo Feb 26 '20 at 05:10
  • @melvin look [here](https://stackoverflow.com/questions/23135114/what-does-the-regex-pattern-pl-do) for more info, explains the use of \\pL a lot better, also I do hope you did remember to escape all the needed characters – Karan Shishoo Feb 26 '20 at 05:13
  • This regex matches only one letter – melvin Feb 26 '20 at 05:18
  • @melvin did you add the `{2,50}` after it ? – Karan Shishoo Feb 26 '20 at 05:19
  • Yes. I have added. I have tested this – melvin Feb 26 '20 at 05:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/208534/discussion-between-karan-shishoo-and-melvin). – Karan Shishoo Feb 26 '20 at 05:20