3

Why does the following method only check the first character in the supplied string?

public static bool IsUnicodeSms(string message)
{
   var strMap = new Regex(@"^[@£$¥èéùìòÇØøÅå_ÆæßÉ!""#%&'()*+,-./0123456789:;<=>? ¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà^{}\[~]|€]+$");
   return !strMap.IsMatch(message);
}

So for example the following string returns false: "abcლ" but "ლabc" returns true.

astralmaster
  • 2,344
  • 11
  • 50
  • 84

2 Answers2

4

You have to escape ] with \] and also put the - at the end:

Change this:

var strMap = new Regex(@"^[@£$¥èéùìòÇØøÅå_ÆæßÉ!""#%&'()*+,-./0123456789:;<=>? ¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà^{}\[~]|€]+$");

To this:

var strMap = new Regex(@"^[@£$¥èéùìòÇØøÅå_ÆæßÉ!""#%&'()*+,./0123456789:;<=>? ¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà^{}\[~\]|€-]+$");

Btw, you can improve your regex and use:

var strMap = new Regex(@"^[@£$¥èéùìòÇØøÅå_ÆæßÉ!"#%&'()*+,./\w:;<=>? ¡ÄÖÑܧ¿äöñüà^{}\[~\]|€-]+$");

And not sure if using the ignore case flag might help you to shorten it a little more like this:

var strMap = new Regex(@"(?i)^[@£$¥èéùìòÇøå_Ææß!"#%&'()*+,./\w:;<=>? ¡§¿äöñüà^{}\[~\]|€-]+$");
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
  • 1
    these regexes don't match empty strings, which may be desirable – Armand Jun 11 '15 at 09:38
  • 1
    @Armand that's very easy to do, just change `]+$");` by `]*$");` – Federico Piazza Jun 11 '15 at 13:09
  • 1
    I know, just didn't want to edit your answer. I also suspect that this doesn't match the newline character, which AFAIK is a valid character. – Armand Jun 11 '15 at 14:00
  • 1
    @Armand well, this question was resolved two months ago I guess OP already solved this question, and also if he marked this answer as the right one I guess it helped too. However, thank you for pointing that. – Federico Piazza Jun 11 '15 at 14:24
  • 3
    Not getting at you. SO is a reference, as well as helping the OP with their problem. These comments are for people referring to the question in future. – Armand Jun 11 '15 at 14:58
2

You copied the code from here.

It's very flawed. It needs more escaping. From Regexp Tutorial - Character Classes or Character Sets:

the only special characters or metacharacters inside a character class are the closing bracket (]), the backslash (\), the caret (^), and the hyphen (-)

So, it needs to be:

new Regex(@"^[@£$¥èéùìòÇØøÅå_ÆæßÉ!""#%&'()*+,\-./0123456789:;<=>? ¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà^{}\[~\]|€]+$");

You can of course improve the regex even further like @Fede demonstrates.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272