user495093
user495093

Reputation:

Validate Unicode Length With Regex

How can I validate ۱۳۹۱/۰۹/۰۹ string with Regex

I want the length of each separate slash be exact as {4}/{2}/{2}

the Unicode range is [\u06F0-\u06F9].

I have problem with length checking.

Upvotes: 3

Views: 353

Answers (2)

Tamer Shlash
Tamer Shlash

Reputation: 9523

This regex should work for you:

"(^|[^\u06F0-\u06F9]{1})[\u06F0-\u06F9]{4}/[\u06F0-\u06F9]{2}/[\u06F0-\u06F9]{2}([^\u06F0-\u06F9]{1}|$)"

Match the date expression under both of the following conditions:

  • Condition1: It should be either at the beginning of the string or after a single character that's not in the character range [\u06F0-\u06F9]

  • Condition2: It should be either at the end of the string or before a single character that's not in the character range [\u06F0-\u06F9]

This will not match the expression in this string:

How can I validate ۱۱۳۹۱/۰۹/۰۹ string with Regex
-------------------^5Numbers, not matched

Or this string:

How can I validate ۱۱۳۹۱/۰۹/۰۹۹ string with Regex
------------------------------^Three numbers, not matched

but still will match the date expression in this string:

How can I validate۱۳۹۱/۰۹/۰۹string with Regex  
------------------^---------^ No whitespaces above ^, the expression is matched though

If you want to avoid this, i.e, just match the date expression alone, with whitespaces (and linebreaks) before and after it, use the following Regex:

(^|[ \t\n]{1})[\u06F0-\u06F9]{4}/[\u06F0-\u06F9]{2}/[\u06F0-\u06F9]{2}([ \t\n]{1}|$)

Hope that's helpful.

Upvotes: 1

João Angelo
João Angelo

Reputation: 57688

You can use the following regular expression:

"^[\u06F0-\u06F9]{4}/[\u06F0-\u06F9]{2}/[\u06F0-\u06F9]{2}$"

You're probably missing the ^ to make it start the match at the beginning of the string and the $ to make it end the match at the end of the string. Without these changes strings that were longer, but that contained your expression would yield as a match.

With this change a match is only successful if the string contains your pattern and does not have any extra characters to the left or to the right of the target pattern.

Upvotes: 5

Related Questions