我需要写一个正则表达式,它将匹配字符串中任意位置的一行自由文本的第一年。
年份将是4位数,将开始20或21(如2030或2199)
它不应该匹配更长的数字,如20304050
下面是我用输出写的一些js代码,正如你所看到的,每个正则表达式只适用于某些情况,但不适用于所有情况。
注意-这个的最终版本不会是JS,所以我不想要需要额外代码的解决方案,只是一个纯正则表达式,尽管我可以接受额外的句号并将结果截断为4个字符。
const values = [
'2025',
'2150 is the year to match',
'the year is 2030 see ref 2099662',
'Should match the year here YEAR_2140 even though it has non numric chars preceeding it',
'Should match the year at the end of a string like this - 2140',
'ref 2099662 the year is 2140. And there is another sentence',
'ref 2099662 the end of the string is the year 2140',
'There is no year here 2055667'
]
console.log(' regx1', 'regx2,', 'regx3,', 'input string')
values.forEach((value, index) => {
value = value.trim()
const regex1 = /2[01]{1}[0-9]{2}/
const regex2 = /2[01]{1}[0-9]{2}[^0-9]{1}/
const regex3 = /2[01]{1}[0-9]{2}[^0-9]{1}/
const year1 = (value.match(regex1) || [])[0] || ' '
const year2 = (value.match(regex2) || [])[0] || ' '
const year3 = (value.match(regex3) || [])[0] || ' '
console.log(`${index + 1}) ${year1}, ${year2}, ${year3}, "${value}",`)
})
此代码输出:
regx1 regx2, regx3, input string
1) 2025, , , "2025",
2) 2150, 2150 , 2150 , "2150 is the year to match",
3) 2030, 2030 , 2030 , "the year is 2030 see ref 2099662",
4) 2140, 2140 , 2140 , "Should match the year here YEAR_2140 even though it has non numric chars preceeding it",
5) 2140, , , "Should match the year at the end of a string like this - 2140",
6) 2099, 2140., 2140., "ref 2099662 the year is 2140. And there is another sentence",
7) 2099, , , "ref 2099662 the end of the string is the year 2140",
8) 2055, , , "There is no year here 2055667",
2条答案
按热度按时间gdx19jrr1#
This pattern works for me:
68bkxrlz2#
我需要写一个正则表达式,它将匹配字符串中任意位置的一行自由文本的第一年。
下面的代码匹配每行中第一次出现的年份。我使用Java来演示它。
印刷品
注意:其他正则表达式引擎可能要求您将
\\
替换为\