Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression in C# for Last Name that includes internal space

Tags:

c#

regex

spaces

I'd like a Regular Expression for C# that matches "Johnson", "Del Sol", or "Del La Range"; in other words, it should match words with spaces in the middle but no space at the start or at the end.

like image 991
Caveatrob Avatar asked Mar 10 '09 21:03

Caveatrob


People also ask

What is regular expression in C?

A regular expression is a sequence of characters that is used to search pattern. It is mainly used for pattern matching with strings, or string matching, etc. They are a generalized way to match patterns with sequences of characters. It is used in every programming language like C++, Java, and Python.

Are there regular expressions in C?

A regular expression is a sequence of characters used to match a pattern to a string. The expression can be used for searching text and validating input. Remember, a regular expression is not the property of a particular language. POSIX is a well-known library used for regular expressions in C.

What is regex h in C?

DESCRIPTION. The <regex. h> header defines the structures and symbolic constants used by the regcomp(), regexec(), regerror() and regfree() functions.


1 Answers

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

This regex has the following features:

  • Will match a one letter last name (e.g. Malcolm X's last name)
  • Will not match last names containing numbers (like anything with a \w or a [^ ] will)
  • Matches unicode letters

But what about last names like "O'Connor" or hyphenated last names ... hmm ...

like image 57
Daniel LeCheminant Avatar answered Oct 02 '22 11:10

Daniel LeCheminant