Home > Backend Development > C++ > How to Create a Reliable Regex for Validating Persian Characters Only?

How to Create a Reliable Regex for Validating Persian Characters Only?

Barbara Streisand
Release: 2025-01-03 21:09:43
Original
593 people have browsed it

How to Create a Reliable Regex for Validating Persian Characters Only?

Regex for Accepting Only Persian Characters

In a scenario where custom form validators require only Persian characters, the following code initially appeared promising:

var myregex = new Regex(@"^[\u0600-\u06FF]+$");
if (myregex.IsMatch(mytextBox.Text))
{
    args.IsValid = true;
}
else
{
    args.IsValid = false;
}
Copy after login

However, it failed to detect specific Persian characters (گ, چ, پ, ژ). This article delves into the issue and provides a solution.

Wrong Assumptions

[u0600-u06FF] and [آ-ی] are incorrect character ranges for Persian validation.

  • [u0600-u06FF] includes 209 unnecessary characters, including Arabic numbers.
  • [آ-ی] encompasses 117 extra characters.

Accurate Character Ranges

To ensure accuracy, the following ranges should be used:

  • Letters:

    • ^[آابپتثجچحخدذرزژسشصضطظعغفقکگلمنوهی] $
    • ^[u0622u0627u0628u067Eu062A-u062Cu0686u062D-u0632u0698u0633-u063Au0641u0642u06A9u06AFu0644-u0648u06CC] $
  • Numbers:

    • ^[۰۱۲۳۴۵۶۷۸۹] $
    • ^[u06F0-u06F9] $
  • Vowels:

    • [ ‬ٌ ‬ًّ ‬َ ‬ِ ‬ُ ‬ْ ‬]
    • [u202Cu064Bu064Cu064E-u0652]

Conclusion

By utilizing the correct character ranges, rejecting invalid Persian characters can be achieved. Additionally, Farsi supports diacritics used in Arabic, but their inclusion is optional during validation.

The above is the detailed content of How to Create a Reliable Regex for Validating Persian Characters Only?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template