Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2332
  • Last Modified:

RegEx pattern for Mexico phone number

I need a RegEx pattern for C# to validate Mexican phone numbers. Can anyone help?
0
dddon123
Asked:
dddon123
  • 7
  • 3
  • 3
2 Solutions
 
b0lsc0ttCommented:
I believe Mexico uses a 10 digit number (eg. 52-XXXXXXXX).  If so use ...

try {
      if (Regex.IsMatch(subjectString, @"52-\d{8}")) {
            // Successful match
      } else {
            // Match attempt failed
      }
} catch (ArgumentException ex) {
      // Syntax error in the regular expression
}

Let me know if the phone number should be in a different format or if you have a question about this.

bol
0
 
dddon123Author Commented:
I think you're right about the 52-, but I read somewhere that the next 8 can be either: 2 digit area code - 6 digit number OR 3 digit area code - 5 digit number. Can you incorporate that, please?
0
 
b0lsc0ttCommented:
I'll assume the seperator is a hyphen for that part too.  If so use ...

      if (Regex.IsMatch(subjectString, @"52-(\d{2}-\d{6}|\d{3}-\d{5})")) {

Let me know how that works or if you have a question.

bol
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
b0lsc0ttCommented:
One improvement to the function is to put characters in to specify the start and end of the string.  Otherwise the expression could match more characters.  Try using ...

      if (Regex.IsMatch(subjectString, @"^52-(\d{2}-\d{6}|\d{3}-\d{5})$")) {

bol
0
 
Fernando SotoCommented:
Hi dddon123;

It is my understanding that since Feb of 2002 that Mexico went from a 8 digit number to a 10 digit number and therefore can no longer dial an 8 digit number and get connected.

Old numbers
    Country Code = 52
    8 Digit number = 12345678
New numbers
    Country Code = 52
    10 Digit number = 1234567890

In order to validate a number you will need to tell us how you users will be entering the numbers. For example with or without the Country Code or 12-3456-7890 or some other format.

Fernando
0
 
b0lsc0ttCommented:
With Fernando's comment I realized that the expressions I provided used the country code which you probably don't need.  I don't have a way to find out exactly what the different number formats will be.  I can confirm the 10 digit number that Fernando mentioned.  From what I found it sounds like the first digits (instead of 52 like I had) may be 55 (followed by 8 digits) or 6 (followed by 9 digits [or maybe 2 digits and then 7]).  I don't know what separator you want to use and where it should be placed.  My expressions above will need some changing and I can help with that if you will be specific about the number format(s).  At the very least the expressions will get you started but let us know what specific formats you want to allow if you need help getting the exact expression to use.

bol
0
 
dddon123Author Commented:
Ok, I found this reference: http://www.wtng.info/wtng-52-mx.html

This is an old page but about mid-way down the page it shows examples of what they call Phase 2 of the Mexico renumbering project. According to this, the format will be either a 2 digit area code followed by a 8 digit subscriber number or a 3 digit area code followed by a 7 digit subscriber number. Note: The area code cannot begin with 0. Using the "-" as a separator, the format of the number should be:
52-nN-NNNN-NNNN or 52-nNN-NNN-NNNN (where n is a non-zero numeric digit). I want to keep the country code in the number also. Attempting to extend what bol gave me, does this look correct:

if (Regex.IsMatch(subjectString, @^52-([1-9]\d{1}-\d{4}-d{4}|[1-9]\d{2}-\d{3}-\d{4})$))

Thanks for the help!
0
 
dddon123Author Commented:
Note: In the pattern of my last post please convert the box looking special characters to double quotes. The double quotes were converted in a copy paste process. Thanks
0
 
Fernando SotoCommented:
Hi dddon123;

Almost perfect you are just missing an escape character, \, before one of the digit mete-characters on the left side of the Or operator, see sample below. I also added the Trim method to the string to make sure that there are no leading or trailing spaces.

if (Regex.IsMatch(subjectString.Trim(), @"^52-([1-9]\d{1}-\d{4}-\d{4}|[1-9]\d{2}-\d{3}-\d{4})$"))

Fernando
0
 
b0lsc0ttCommented:
dddon123,

It seems like Fernando found the only error.  It worked great in the tests I made and I can't see any problems.  The suggestion to use trim is a good one but the use of ^ and $ in the expression should also work to prevent leading or trailing spaces.

Let us know if you have a question or if there is any thing else you need with this.  Thanks for providing those formatting details. :)

bol
0
 
Fernando SotoCommented:
Hi bol;

In your statement, "The suggestion to use trim is a good one but the use of ^ and $ in the expression should also work to prevent leading or trailing spaces." The use of ^ and $ indicate in the case of ^ that the match must happen at the beginning of the string and the $ states that the match must be at the end of the string but does not remove spaces. So for example the following string would not match the regex pattern, "  52-12-3456-7890  " but the string "52-12-3456-7890" does match the pattern.

Fernando
0
 
b0lsc0ttCommented:
@Fernando - I see now that the comment was a little confusing.  Thanks for your reply to clear it up and the details you used.  I do know what those characters (^ and $) mean in an expression but did not completely think through the difference until you posted.  As you pointed out using just those characters would fail on the first example ("  52-12-3456-7890  ") while using trim would make it pass.  If you want to permit leading/trailing spaces (to be stripped later) then just ^ and $ in the expression isn't enough.  My comment overlooked that difference.

Dddon123, if you do use trim in that expression then just make sure you use it again to strip the spaces so they don't sneak through. :)

Thanks again for clearing that up.

bol
0
 
b0lsc0ttCommented:
Thanks for the fun question, the grade, and the points.  I'm glad I could help.

bol
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now