简体   繁体   English

正则表达式在数字之后或之前立即查找单词

[英]Regex to find immediately a word after or before the a number

I want to find the month in a date (string format) that is immediately before or after the year (which is expressed always in 4 digits).我想在紧接在年份之前或之后的日期(字符串格式)中找到月份(始终以 4 位数字表示)。 For instance例如

"Issued May 2018" -> May “2018 年 5 月发行”-> 5 月

"Issued 2018 May" -> May “2018 年 5 月发行”-> 5 月

"Rilasciato ad Agosto 2018" -> Agosto “Rilasciato ad Agosto 2018”-> Agosto

"Data di scadenza: Agosto 2018" -> Agosto "Data di scadenza: Agosto 2018" -> Agosto

I put here different combinations because I have strings with different languages, and in every country has its different way to put the data format (month followed by year or vice versa)我在这里放了不同的组合,因为我有不同语言的字符串,并且每个国家都有不同的方式来放置数据格式(月后年,反之亦然)

Is it possible to detect the month (that can be expressed in different languages) using the regex?是否可以使用正则表达式检测月份(可以用不同的语言表示)?

I don't think it's the best way to do it but you can try to use something like:我认为这不是最好的方法,但您可以尝试使用以下方法:

(([A-Z]\w+)(\s\d{4}))|((\d{4}\s)([A-Z]\w+))

It's matching with the following conditions:它符合以下条件:

  • The month is next to the year (4 digits format)月份紧邻年份(4 位数字格式)
  • There is a whitespace between month and year月份和年份之间有一个空格
  • The month start with a capital letter月份以大写字母开头

But pay attention to the whole string because if you have something like: "Issued 2018 May" The regex will match with the wrong word due to the capital letter in "Issued"但请注意整个字符串,因为如果您有类似以下内容:“Issued 2018 May” 由于“Issued”中的大写字母,正则表达式将匹配错误的单词

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM