简体   繁体   English

一个单词后跟一个序列中的多个数字的正则表达式

[英]Regex for a word followed by multiple numbers in a sequence

I am trying to create a general regular expression that returns a boolean if it sees one word followed but more than one set of numbers.我正在尝试创建一个通用正则表达式,如果它看到后面跟着一个单词但不止一组数字,它会返回 boolean。

If should report a TRUE for the following test cases:如果应该为以下测试用例报告 TRUE:

"AB 40 256 556 1144 1296 1496 1722 1847 1915 1979 2018 2056 2106 2240 2294 2394 2539 2587 2660"  
"SB 466 848 929 1339 1554 1761 1807 1828 1852 1875 1899 1922 1940 1968 2007 2046 2074 2075 2158"
"Assembly 772 1604 1932 2187 2543 2759 2777"
"Senate 241 1110 1342 1822 1865 1957"

And FALSE for the following cases:对于以下情况为 FALSE:

"ACR 105"                                                                                       
"SJR 29" 
"AB 2359 AB 2456 and AB 2823" 
"CDFA Budget for Pierce's Disease"
"PERS, STRS, Regents"

If you can provide two answers : one regular expression looking for the letters and the numbers and another answer looking for multiple numbers back to back, I would greatly appreciate it.如果您能提供两个答案:一个正则表达式查找字母和数字,另一个答案查找多个背靠背的数字,我将不胜感激。

Thank you so much for your help!非常感谢你的帮助!

Try this.尝试这个。

grepl('\\D+\\d+\\s\\d+', x)
# [1]  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE

Explanation解释

  • \D+ one or more non-digit \D+一个或多个非数字
  • \d+ one or more digit \d+一个或多个数字
  • \s whitespace \s空格

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

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