简体   繁体   中英

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.

If should report a TRUE for the following test cases:

"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:

"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+ one or more digit
  • \s whitespace

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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