Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for Name, Streetname, Cityname, etc

I m programming a web application with asp.net mvc and c#. In a form a user should enter a name, a streetname and a city in different fields.

  1. Start: The entered value has to start with an 'alphabetic' character (no matter if the language is english, chinese or french or what ever. So things like é and chinese chars and so on are ok. But chars like *¥@#1 and so on are not allowed)
  2. Middle: The same as i said first and spaces (but not two spaces after eachother).
  3. End: That what I have said for the start.

This is correct:

  • A b c

  • Abcd ef

  • Abcdef

This is not correct:

  • 1abc

  • A1 bc

  • 1 2 3

  • a b c (space at the start)

Question:

  • What is the correct regex for this?

  • How can i set the length?

  • In a second case I want to allow numbers 0123456789 too (like the chars)

This is what I have: '^[a-zA-Z][a-zA-Z ][a-zA-Z]$'

Thank you

like image 788
juliushuck Avatar asked Oct 27 '18 16:10

juliushuck


2 Answers

You want to validate strings that only contain letter words separated with a single space between them.

You may use a regex like

^\p{L}+(?: \p{L}+)*$

Or, if any whitepsace is allowed:

^\p{L}+(?:\s\p{L}+)*$

See the regex demo

To make it only match strings of 3 or more chars, use

^(?=.{3})\p{L}+(?:\s\p{L}+)*$
 ^^^^^^^^

Details

  • ^ - start of a string
  • (?=.{3}) = a positive lookahead that requires any 3 chars immediately after the start of a string
  • \p{L}+ - 1 or more any Unicode letters
  • (?:\s\p{L}+)* - zero or more repetitions of
    • \s - any whitespace
    • \p{L}+ - 1 or more any Unicode letters
  • $ - end of string

Note that if you need to use it in ASP.NET, only use this regex to validate on the server side, as on the client side, this pattern might not be correctly handled by JavaScript regex.

like image 152
Wiktor Stribiżew Avatar answered Sep 30 '22 03:09

Wiktor Stribiżew


You can use this regex:

^(?:\p{L}+ )*\p{L}+$

\p{L} matches all unicode code points that are in the "Letters" category.

The regex matches 0 or more of \p{L}+ (one or more letters plus a space) and then ensures there is at least one or more letters.

Demo

Example code:

Console.WriteLine(Regex.IsMatch("abc def", @"^(?:\p{L}+ )*\p{L}+$"));
like image 33
Sweeper Avatar answered Sep 30 '22 01:09

Sweeper