前进之路 发表于 2024-7-30 12:30:42

c#中的正则表达式和日期的利用(超全)



在 C# 中,正则表达式(Regular Expressions)是一种强大的文本处理工具,用于实行各种字符串搜刮、更换和验证使命。以下是一些常用的正则表达式示例及其用途:
1. 邮箱地址验证


string emailPattern = @"^[^@\s]+@[^@\s]+\.[^@\s]+$"; 2. URL 验证


string urlPattern = @"^(https?:\/\/)?([\da-z\.-]+)\.({2,6})([\/\w \.-]*)*\/?$"; 3. 电话号码验证(简单示例)


string phonePattern = @"^\+?(\d{1,3})?[-. ]?\(?(\d{3})\)?[-. ]?(\d{3})[-. ]?(\d{4})$"; 4. 身份证号码验证(中国)


string idCardPattern = @"^\d{5}(18|19|20)\d{2}(0|1)(0|\d|3)\d{3}$"; 5. IP 地址验证


string ipPattern = @"^((25|2|??)\.){3}(25|2|??)$"; 6. 空白行检测


string blankLinePattern = @"^\s*$"; 7. 十六进制颜色代码验证


string hexColorPattern = @"^#(?:{3}){1,2}$"; 8. 邮政编码验证(中国)


string postalCodePattern = @"^{6}$"; 9. 只包含字母和数字的字符串验证


string alphanumericPattern = @"^+$"; 10. 匹配 HTML 标签


string htmlTagPattern = @"<(.*)>.*<\/\1>"; 利用正则表达式示例

以下是一个利用正则表达式检查字符串是否为有效电子邮件地址的示例:

using System;
using System.Text.RegularExpressions;

class Program
{
 static void Main()
 {
     string email = "example@example.com";
     bool isValid = Regex.IsMatch(email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$");
     Console.WriteLine(isValid ? "Valid email." : "Invalid email.");
 }
}
datetime

DateTime 类在 C# 中提供了大量的 API 来处理日期和时间。以下是一些常用的 DateTime API:
属性



[*] Now: 获取当前本地日期和时间。
[*] UtcNow: 获取和谐世界时(UTC)的当前日期和时间。
[*] Today: 获取本日的日期,时间部分为零点。
构造函数



[*] DateTime(): 创建一个 DateTime 对象,默以为当前时间。
[*] DateTime(long ticks): 从自 0001 年 1 月 1 日以来的滴答数创建 DateTime 对象。
[*] DateTime(int year, int month, int day): 创建指定年、月、日的 DateTime 对象,时间部分默以为零点。
[*] DateTime(int year, int month, day, int hour, int minute): 创建指定年、月、日、时、分的 DateTime 对象。
方法



[*] Add(TimeSpan): 返回一个新的 DateTime 对象,它是将指定的 TimeSpan 值添加到当前对象的时间。
[*] AddDays(double): 返回一个新的 DateTime 对象,它是将指定天数添加到当前对象的时间。
[*] AddHours(double): 返回一个新的 DateTime 对象,它是将指定小时数添加到当前对象的时间。
[*] AddMilliseconds(double): 返回一个新的 DateTime 对象,它是将指定毫秒数添加到当前对象的时间。
[*] AddMinutes(double): 返回一个新的 DateTime 对象,它是将指定分钟数添加到当前对象的时间。
[*] AddMonths(int): 返回一个新的 DateTime 对象,它是将指定月份数添加到当前对象的时间。
[*] AddSeconds(double): 返回一个新的 DateTime 对象,它是将指定秒数添加到当前对象的时间。
[*] AddYears(int): 返回一个新的 DateTime 对象,它是将指定年份数添加到当前对象的时间。
[*] CompareTo(Object): 比较两个 DateTime 对象的顺序。
[*] Equals(Object): 判定两个 DateTime 对象是否相称。
[*] IsDaylightSavingTime(): 判定当前 DateTime 对象是否处于夏令时。
[*] Subtract(TimeSpan): 返回一个新的 DateTime 对象,它是当前对象的时间减去指定的 TimeSpan 值。
[*] ToString(): 将 DateTime 对象转换为字符串。
[*] ToString(String): 利用指定的格式字符串将 DateTime 对象转换为字符串。
[*] ToLocalTime(): 将 DateTime 对象从 UTC 时间转换为本地时间。
[*] ToUniversalTime(): 将 DateTime 对象从本地时间转换为 UTC 时间。
静态方法



[*] DaysInMonth(int year, int month): 确定指定年份和月份的天数。
[*] IsLeapYear(int year): 确定指定年份是否为闰年。
操作符



[*] == 和 !=: 比较两个 DateTime 对象是否相称或不相称。
[*] -: 从 DateTime 对象中减去 TimeSpan 对象。
[*] > 和 <: 比较两个 DateTime 对象的大小。
格式化方法



[*] DateTimeFormatInfo 类提供了格式化 DateTime 对象的方法,例如 GetMonthName(int) 和 GetDayName(int)。
利用这些 API,你可以实行日期和时间的算术、格式化、比较和转换等操作。例如:

DateTime now = DateTime.Now;
DateTime tomorrow = now.AddDays(1);
int daysInMonth = DateTime.DaysInMonth(2024, 7);
bool isLeapYear = DateTime.IsLeapYear(2024);
string formattedDate = now.ToString("yyyy-MM-dd");


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: c#中的正则表达式和日期的利用(超全)