Regex doesn't match all foreign characters

Viewed 159

Here's my regex ^([\\p{L}-|a-zA-Z0-9-_]+)$ and it is supposed to allow all foreign letters as well as numeric letter, number. But for some reason, hindi characters cannot match.

I wrote a Xunit test to prove.

[Fact]
        public void test()
        {
            var hindiChar = "इम्तहान";
            var input = "12345ABCDPrüfungテスト中文테스트إسرائيل" + hindiChar;
            var regex = "^([\\p{L}-|a-zA-Z0-9-_]+)$";
            Assert.True(new Regex(regex).IsMatch(input));
        }

If you remove the hindiChar, the test will return true; but if you add the hindiChar, the test will return false.

I thought part of the regex is to fit all foreign characters, but not sure why it doesn't match hindi characters.

1 Answers

It is not enough to use \p{L} to match words, you also need to match diacritics. That can be done by adding \p{M} to your regex. Note that even the \w shorthand "word" character class in .NET regex by default also matches a set of diacritics, \p{Mn} (Mark, Nonspacing Unicode char category), see this .NET regex reference. However, here you need \p{M} to allow any diacritics.

Note that | inside a character class matches a literal | char, so you need to remove the | from your pattern.

It seems to me you use

@"^[\p{L}\p{M}0-9_-]+$"

It will match any string of one or more letters, diacritics, ASCII digits, _ or - chars.

See the regex demo.

Note that in case you want to allow any Unicode digit chars, you may even use

@"^[\w\p{M}-]+$"

See another demo

Related