Regex for accepting Persian characters in address

551 views Asked by At

Here is my regex for testing address:

^((([\u0600-\u06FF])+\s?([0-9()،,-]?)+\s?))+$

but when address contains / or \ character, RegexMatchTimeoutException occurs.

here is sample input:

"تهران - خیابان سهروردی - خیابان 19 شرقی، کوچه 59, پلاک 39525، / طبقه ٣"

1

There are 1 answers

0
Wiktor Stribiżew On BEST ANSWER

The problem with your regex is that it contains nested quantifiers that quantify optional patterns.

Use linear logic:

^[\u0600-\u06FF]+(?:[\s0-9()،,-]+[\u0600-\u06FF]+)*$

See the regex demo

Details:

  • ^ - start of string
  • [\u0600-\u06FF]+ - 1 or more symbols from the given Unicode range
  • (?:[\s0-9()،,-]+[\u0600-\u06FF]+)* - 0+ sequences of:
    • [\s0-9()،,-]+ - 1+ symbols: either whitespace, digits, (, ), ،, , or -
    • [\u0600-\u06FF]+ - 1 or more symbols from the given Unicode range
  • $ - end of string.