mirror of
https://github.com/chylex/.NET-Community-Toolkit.git
synced 2025-04-10 11:15:45 +02:00
Regex to TryParse
This commit is contained in:
parent
f20a2c29fd
commit
8ddf86d948
@ -10,6 +10,7 @@
|
||||
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
|
||||
// ******************************************************************
|
||||
|
||||
using System.Globalization;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Microsoft.Toolkit.Extensions
|
||||
@ -19,8 +20,6 @@ namespace Microsoft.Toolkit.Extensions
|
||||
/// </summary>
|
||||
public static class StringExtensions
|
||||
{
|
||||
internal const string DecimalRegex = "^-?[0-9]{1,28}([.,][0-9]{1,28})?$";
|
||||
internal const string NumberRegex = "^-?[0-9]{1,9}$";
|
||||
internal const string PhoneNumberRegex = @"^\s*\+?\s*([0-9][\s-]*){9,}$";
|
||||
internal const string CharactersRegex = "^[A-Za-z]+$";
|
||||
internal const string EmailRegex = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
|
||||
@ -41,7 +40,7 @@ public static bool IsEmail(this string str)
|
||||
/// <returns><c>true</c> for valid decimal number.<c>false</c> otherwise</returns>
|
||||
public static bool IsDecimal(this string str)
|
||||
{
|
||||
return Regex.IsMatch(str, DecimalRegex);
|
||||
return decimal.TryParse(str, NumberStyles.Number, CultureInfo.InvariantCulture, out decimal _decimal);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -50,7 +49,7 @@ public static bool IsDecimal(this string str)
|
||||
/// <returns><c>true</c> for valid integer.<c>false</c> otherwise</returns>
|
||||
public static bool IsNumeric(this string str)
|
||||
{
|
||||
return Regex.IsMatch(str, NumberRegex);
|
||||
return int.TryParse(str, out int _integer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
Loading…
Reference in New Issue
Block a user