Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading