Skip to content

Instantly share code, notes, and snippets.

@kleinron
Last active April 27, 2023 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kleinron/fed76198cd4661cc4b2a488ee891de99 to your computer and use it in GitHub Desktop.
Save kleinron/fed76198cd4661cc4b2a488ee891de99 to your computer and use it in GitHub Desktop.
Pattern phoneNumber = Pattern.With
.AtBeginning.Choice.Either(
Pattern.With.Literal("02"),
Pattern.With.Literal("03"),
Pattern.With.Literal("04"),
Pattern.With.Literal("08"),
Pattern.With.Literal("09"),
Pattern.With.Literal("072"),
Pattern.With.Literal("073"),
Pattern.With.Literal("074"),
Pattern.With.Literal("076"),
Pattern.With.Literal("077"),
Pattern.With.Literal("050"),
Pattern.With.Literal("052"),
Pattern.With.Literal("054")
)
.Digit.Repeat.InRange(6,7).AtEnd;
Pattern phoneNumber = Pattern.With
.AtBeginning.Choice.Either(
Pattern.With.Literal("02"),
Pattern.With.Literal("03"),
Pattern.With.Literal("04"),
Pattern.With.Literal("08"),
Pattern.With.Literal("09"),
Pattern.With.Literal("072"),
Pattern.With.Literal("073"),
Pattern.With.Literal("074"),
Pattern.With.Literal("076"),
Pattern.With.Literal("077"),
Pattern.With.Literal("050"),
Pattern.With.Literal("052"),
Pattern.With.Literal("054")
)
.WhiteSpace.Repeat.Optional
.Literal("-").Repeat.Optional
.WhiteSpace.Repeat.Optional
.Digit.Repeat.InRange(6,7).AtEnd;
^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angle)>)$
^(([0]([2|3|4|8|9|72|73|74|76|77])))[2-9]\d{6,7}$
When you work with SharpKit, you write C# instead of JavaScript
Some people, when confronted with a problem, think "I know, I'll use regular expressions."
Now they have two problems.
namespace Electron.Validation
{
public static class CharExt
{
public static bool IsDigit(this char c)
{
return c >= '0' && c <= '9';
}
}
public class PhoneNumber
{
public static bool IsValid(string s)
{
if (null == s)
return false;
var validPrefixes = new[] {
"02",
"03",
"04",
"08",
"09",
"072",
"073",
"074",
"076",
"077",
"050",
"052",
"054"
};
int prefixLength = 0;
var foundMatchingPrefix = false;
foreach (var validPrefix in validPrefixes)
if (s.StartsWith(validPrefix))
{
prefixLength = validPrefix.Length;
foundMatchingPrefix = true;
break;
}
if (!foundMatchingPrefix)
return false;
// remaining length should be between 6 and 7
var remainingLength = s.Length - prefixLength;
if (!((6 == remainingLength) || (7 == remainingLength)))
return false;
// check the rest of the string to be digits
for (int i = prefixLength; i < s.Length; i++)
if (!s[i].IsDigit())
return false;
return true;
}
}
}
public class PhoneNumber
{
public static bool IsValid(string s)
{
if (null == s)
return false;
var validPrefixes = new[] {
"02",
"03",
"04",
"08",
"09",
"072",
"073",
"074",
"076",
"077",
"050",
"052",
"054"
};
int prefixLength = 0;
var foundMatchingPrefix = false;
foreach (var validPrefix in validPrefixes)
if (s.StartsWith(validPrefix))
{
prefixLength = validPrefix.Length;
foundMatchingPrefix = true;
break;
}
if (!foundMatchingPrefix)
return false;
if (s.Length == prefixLength)
return false;
int nextDigitIndex;
if (s[prefixLength] == ' ')
nextDigitIndex = prefixLength + 1;
else
nextDigitIndex = prefixLength;
// remaining length should be between 6 and 7
var remainingLength = s.Length - nextDigitIndex;
if (!((6 == remainingLength) || (7 == remainingLength)))
return false;
// check the rest of the string to be digits
for (int i = nextDigitIndex; i < s.Length; i++)
if (!s[i].IsDigit())
return false;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment