2

I'm looking for a regex that that matches both English numbers (1234567890) and Persian numbers (۱۲۳۴۵۶۷۸۹۰).

The regex in the accepted answer of this question "Regex for persian number" also matches Persian letters such as (ا ب پ ت ث ج و ...), but I only want it to match Persian numbers.

Arad Alvand
  • 8,607
  • 10
  • 51
  • 71
  • @WiktorStribiżew This is not duplicate. he regex in the accepted answer of this question "Regex for persian number" also matches Persian letters such as (ا ب پ ت ث ج و ...), but I only want it to match Persian numbers. – Arad Alvand Jun 22 '19 at 21:17
  • It is closed correctly, the [`[\u06F0-\u06F90-9]`](https://regex101.com/r/tDIA65/1) regex does not match letters, `\u06F0-\u06F9` only matches the [digits](https://r12a.github.io/uniview/?charlist=%DB%B1%DB%B2%DB%B3%DB%B4%DB%B5%DB%B6%DB%B7%DB%B8%DB%B9%DB%B0). It is much better to use Unicode escapes rather than literal chars because of encodings. I have seen people asking question here when the pattern contained literal Hebrew chars, for example, and using `\uXXXX` notation solved the issue. – Wiktor Stribiżew Jun 22 '19 at 21:18

1 Answers1

4

The shortest and most elegant RegEx for this would be:

[۰۱۲۳۴۵۶۷۸۹0-9]
Arad Alvand
  • 8,607
  • 10
  • 51
  • 71
  • And what if there is some text with mixed digits? E.g. "۴۵43۶5۷۸". – alx Jun 22 '19 at 21:08
  • @alx This regex also matches such case, however, as far as I know, if you don't want to allow expressions like that, you could check for `[۱۲۳۴۵۶۷۸۹۰]` and `[0-9]` separately. If one matches the expression, the other one shouldn't. – Arad Alvand Jun 22 '19 at 21:13
  • No, I mean, in does that case make any sense? Is it a common practice (or even a rare but legit case) when these digits can be mixed? E.g. arabic and roman numbers are usually not mixed (e.g. you can count there will be at least some punctuation or space as separator). – alx Jun 22 '19 at 21:17
  • 1
    @alx it basically makes sense, because they're all valid digits, but in different languages. I'd say it's "a rare but legit case" as you mentioned. – Arad Alvand Jun 22 '19 at 21:19
  • 1
    Okay, thank you, good to know. – alx Jun 22 '19 at 21:20