Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression for French characters

Tags:

regex

php

I need a function or a regular expression to validate strings which contain alpha characters (including French ones), minus sign (-), dot (.) and space (excluding everything else)

Thanks

like image 927
nextu Avatar asked Dec 17 '09 14:12

nextu


People also ask

What is the regular expression for characters?

A regular expression (sometimes called a rational expression) is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. “find and replace”-like operations.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

What does $1 do in regex?

For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.

What is A+ in regular expression?

The character + in a regular expression means "match the preceding character one or more times". For example A+ matches one or more of character A. The plus character, used in a regular expression, is called a Kleene plus . Regular Expression. Matches.


2 Answers

/^[a-zàâçéèêëîïôûùüÿñæœ .-]*$/i

Use of /i for case-insensitivity to make things simpler. If you don't want to allow empty strings, change * to +.

like image 142
Amber Avatar answered Oct 19 '22 23:10

Amber


Simplified solution:

/^[a-zA-ZÀ-ÿ-. ]*$/

Explanation:

^ Start of the string [ ... ]* Zero or more of the following: a-z lowercase alphabets A-Z Uppercase alphabets À-ÿ Accepts lowercase and uppercase characters including letters with an umlaut - dashes . periods spaces $ End of the string

like image 36
Sam G Avatar answered Oct 20 '22 01:10

Sam G