C# 匹配日期的正则表达式(月日、年或年月日)
我正在尝试编写一个正则表达式,用于在字符串中查找日期,该字符串前面(或后面)可能有空格、数字、文本、行尾等。该表达式应处理以下两种格式的日期格式: 1) 月份名称日期,年份-即2019年1月10日或C# 匹配日期的正则表达式(月日、年或年月日),c#,regex,regex-lookarounds,regex-group,regex-greedy,C#,Regex,Regex Lookarounds,Regex Group,Regex Greedy,我正在尝试编写一个正则表达式,用于在字符串中查找日期,该字符串前面(或后面)可能有空格、数字、文本、行尾等。该表达式应处理以下两种格式的日期格式: 1) 月份名称日期,年份-即2019年1月10日或 2) 年月日-即19年11月30日 我找到了这个月名,日,年 (Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?
2) 年月日-即19年11月30日 我找到了这个月名,日,年
(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4}
(感谢这里的维弗克
这适用于mm/dd/yy(以及m/d/y的各种组合)
(感谢Steven Levithan和Jan Goyvaerts
我试着这样把它们结合起来
((Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4})|((1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2})
当我在输入字符串“2019年1月1日支付”中搜索“on[regex over]”时,它会找到日期,但不会找到“on”这个词。如果我只使用
(1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2}
有人能看出我做错了什么吗
编辑
我正在使用下面的c#.net代码:
string stringToSearch = "Paid on 1/1/2019";
string searchPattern = @"on ((Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4})|((1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2})";
var match = Regex.Match(stringToSearch, searchPattern, RegexOptions.IgnoreCase);
string foundString;
if (match.Success)
foundString= stringToSearch.Substring(match.Index, match.Length);
例如string searchPattern = @"on ((Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4})|((1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2})";
stringToSearch = "Paid on Jan 1, 2019";
found = "on Jan 1, 2019" -- worked as expected, found the word "on" and the date
stringToSearch = "Paid on 1/1/2019";
found = "1/1/2019" -- did not work as expected, found the date but did not include the word "on"
如果我改变模式
string searchPattern = @"on ((1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2})|((Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4})"";
stringToSearch = "Paid on Jan 1, 2019";
found = "Jan 1, 2019" -- did not work as expected, found the date but did not include the word "on"
stringToSearch = "Paid on 1/1/2019";
found = "on 1/1/2019" -- worked as expected, found the word "on" and the date
谢谢您的表达式似乎工作正常,两者都可以。如果您希望在目标输出之前或之后捕获任何内容,只需在左侧和右侧添加两个边界即可。例如,请查看: 例如,您可以添加两个类似于
(.*)
的组,并将原始表达式包装在一个组中,这样做
正则描述图
该图显示了表达式的工作方式,您可能希望在以下情况下测试其他表达式:
C#测试
JavaScript演示
此JavaScript演示显示了表达式的工作原理:
(1[0-0-0-0-2[1-1-9[1[1[1[1[1[1[1[12[0-1[12[0-12[0-9[1-1[1-1-1[1-9[1-1[1-1[1-1-9[1-0-0-0-9[1-1-1-1-1[1-1[1-1-1-1-9])/////((:::::[0-0-0-0-0-0-0-0-9[0-0-9[0-2{{{{{{{上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述政府政府政府政府政府政府政府政府政府政府当局))及(((((((1[1[1[1[1[0-0-0-0-0-0-0-0-0-0-[s+\d{4})(.*)/gm;
const str=` 2019年1月1日支付,以及您希望在2019年1月1日之后获得的任何其他物品
2019年1月1日支付,以及您希望在`之后获得的任何其他物品;
常量subst=`\n组1:$1\n组2:$2\n组3:$3\n组4:$4`;
//被替换的值将包含在结果变量中
const result=str.replace(regex,subst);
console.log('Substitution result:',result);似乎可以使用正则表达式。如果您使用java,请将代码链接到我。根据正则表达式的差异判断,您必须将所有反斜杠加倍:
\
\
(在字符串文本中,\
用于表示一个反斜杠)。什么是编程语言?抱歉@sln,我的问题不准确。正则表达式确实找到了日期,但单词“on”不是结果的一部分。我正在使用c#.net。我将编辑我的问题以澄清。感谢@Emma建议包含输入和输出。这是我的第一篇帖子(我已经阅读了很多其他帖子)非常感谢您提出的改进/澄清我的问题的建议。非常感谢您-这正是我所需要的,样本和测试代码非常有用。您如何匹配返回日期?此返回“adfadf 2021年1月1日”
string searchPattern = @"on ((1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2})|((Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4})"";
stringToSearch = "Paid on Jan 1, 2019";
found = "Jan 1, 2019" -- did not work as expected, found the date but did not include the word "on"
stringToSearch = "Paid on 1/1/2019";
found = "on 1/1/2019" -- worked as expected, found the word "on" and the date
(.*)(((1[0-2]|0?[1-9])\/(3[01]|[12][0-9]|0?[1-9])\/(?:[0-9]{2})?[0-9]{2})|((Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4}))(.*)
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(.*)(((1[0-2]|0?[1-9])\/(3[01]|[12][0-9]|0?[1-9])\/(?:[0-9]{2})?[0-9]{2})|((Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4}))(.*)";
string input = @"Paid on Jan 1, 2019 And anything else that you wish to have after
Paid on 1/1/2019 And anything else that you wish to have after";
RegexOptions options = RegexOptions.Multiline;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}