求一个正则表达式 在线等ing
人物生日数据时间格式是 1986/9/27 0:00:00
通过正则表达式怎么判断此人是否是今天生日
高手指教
[解决办法]
校验日期的函数1.校验日期<script language=javascript>function isdate(str){var r = str.match(/^(\d{1,4})(-\/)(\d{1,2})\2(\d{1,2})$/);if(r==null)return false; var d = new date(r[1], r[3]-1, r[4]);return (d.getfullyear()==r[1]&&(d.getmonth()+1)==r[3]&&d.getdate()==r[4]);}alert(isdate("2002-01-31"));alert(isdate("2002-01-41"));</script>2.校验日期加时间<script language=javascript>function istime(str){var r = str.match(/^(\d{1,4})(-\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);if(r==null)return false; var d = new date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);return (d.getfullyear()==r[1]&&(d.getmonth()+1)==r[3]&&d.getdate()==r[4]&&d.gethours()==r[5]&&d.getminutes()==r[6]&&d.getseconds()==r[7]);}alert(istime("2002-1-31 12:34:56"));alert(istime("2001-2-29 12:54:56"));alert(istime("2002-1-41 12:00:00"));</script>
[解决办法]
正则:
string s = "1986/4/11 0:00:00"; Match match = Regex.Match(s, @"\d{4}/(?<month>\d{1,2})/(?<day>\d{1,2})"); if (match.Groups["month"].Value == DateTime.Now.Month.ToString() && match.Groups["day"].Value == DateTime.Now.Day.ToString()) Response.Write("生日快乐!");
[解决办法]
Regex re = new Regex(@"/[\d]{1,2}/[\d]{1,2}"); string sr = "1986/4/11 0:00:00"; string a = re.Matches(sr)[0].ToString(); string b = re.Matches(DateTime.Now.ToShortDateString())[0].ToString(); if (a==b) { Console.WriteLine("Happy"); }
[解决办法]
[code=Java][/code]
String sr = "(\\d{4})([09]|[9])[27]";
Pattern p = Pattern.compile(sr);
Matcher m = p.matcher("20100927");
if(m.find()){
System.out.println("OK");
}else{
System.out.println("NO");
}
[解决办法]
[code=Java][/code]
String sr = "(\\d{4})([09]|[9])[27]";
Pattern p = Pattern.compile(sr);
Matcher m = p.matcher("20100927");
if(m.find()){
System.out.println("OK");
}else{
System.out.println("NO");
}
[解决办法]
string time="1986/9/27 0:00:00"; Regex reg=new Regex(@"\d{4}/(\d{1,2})/(\d{1,2})"); if(reg.IsMatch(time) && reg.Match(time).Groups[1].Value==DateTime.Now.Month.ToString() && reg.Match(time).Groups[2].Value==DateTime.Now.Day.ToString() ) { Console.WriteLine("Happy Birthday"); }
[解决办法]
[code=Java][/code]// 1986/9/27 0:00:00
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm");
String s="1986/9/27 0:00:00";
String sraaa = sdf.format(new Date(s));
String sr = "(\\d{4})([" + sraaa.substring(4,6) + "])[" + sraaa.substring(6,8) + "]";
System.out.println(sr);
//String sr = "(\\d{4})([09]|[9])[27]";
Pattern p = Pattern.compile(sr);
Matcher m = p.matcher("20100927");
if(m.find()){
System.out.println("OK");
}else{
System.out.println("NO");
}
[解决办法]
string time="1986/9/27 0:00:00"; Regex reg=new Regex(@"\d{4}/(\d{1,2})/(\d{1,2})"); if(reg.IsMatch(time) && reg.Match(time).Groups[1].Value==DateTime.Now.Month.ToString().PadLeft(2,'0') && reg.Match(time).Groups[2].Value==DateTime.Now.Day.ToString().PadLeft(2,'0')) { Console.WriteLine("Happy Birthday"); }
[解决办法]
写的有点乱整理下
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm"); String str_input = "1986/9/27 0:00:00"; String str_do = sdf.format(new Date(str_input)); String str_pattern = "(\\d{4})([" + str_do.substring(4,6) + "])[" + str_do.substring(6,8) + "]"; Pattern pattern = Pattern.compile(str_pattern); //matcher里的参数换成当前日期 Matcher m = pattern.matcher("20100927"); if(m.find()){ System.out.println("OK"); }else{ System.out.println("NO"); }
[解决办法]
string partten="^[0-9]{4}/" + datetime.Now.Month + "/"+datetime.Now.day;