{"id":636,"date":"2014-06-12T17:30:20","date_gmt":"2014-06-12T22:30:20","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=636"},"modified":"2014-06-12T17:30:20","modified_gmt":"2014-06-12T22:30:20","slug":"extract-all-numbers-from-a-string","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2014\/06\/12\/extract-all-numbers-from-a-string\/","title":{"rendered":"extract all numbers from a string"},"content":{"rendered":"<pre style=\"color: #000000;\">Thanks *VERY* much, this is great!\n\nI realized a few more cases, I think I've got something that \ncovers all the possibilities now:\n\n\nlibrary(stringr)\ntmpstr = \"The first number is: 32.  Another one is: 32.1. \nHere's a number in scientific format, 0.3523e10, and \nanother, 0.3523e-10, and a negative, -313.1\"\n\npatternslist = NULL\np=0\npatternslist[[(p=p+1)]] = \"(\\\\d+)\"\t\t\t\t# positive integer\npatternslist[[(p=p+1)]] = \"(-\\\\d+)\"\t\t\t\t# negative integer\npatternslist[[(p=p+1)]] = \"(\\\\d+\\\\.\\\\d+)\"\t\t# positive float\npatternslist[[(p=p+1)]] = \"(\\\\d+\\\\.\\\\d+e\\\\d+)\"\t# positive \nfloat, scientific w. positive power\npatternslist[[(p=p+1)]] = \"(\\\\d+\\\\.\\\\d+e-\\\\d+)\" # positive \nfloat, scientific w. negative power\npatternslist[[(p=p+1)]] = \"(-\\\\d+\\\\.\\\\d+)\"\t\t# negative float\npatternslist[[(p=p+1)]] = \"(-\\\\d+\\\\.\\\\d+e\\\\d+)\"\t# negative \nfloat, scientific w. positive power\npatternslist[[(p=p+1)]] = \"(-\\\\d+\\\\.\\\\d+e-\\\\d+)\"# negative \nfloat, scientific w. negative power\n\npatternslist[[(p=p+1)]] = \"(\\\\d+e\\\\d+)\"\t\t\t# positive int, \nscientific w. positive power\npatternslist[[(p=p+1)]] = \"(\\\\d+e-\\\\d+)\" \t\t# positive int, \nscientific w. negative power\npatternslist[[(p=p+1)]] = \"(-\\\\d+e\\\\d+)\"\t\t# negative int, \nscientific w. positive power\npatternslist[[(p=p+1)]] = \"(-\\\\d+e-\\\\d+)\"\t\t# negative int, \nscientific w. negative power\n\npattern = paste(patternslist, collapse=\"|\", sep=\"\")\npattern\nas.numeric(str_extract_all(tmpstr,pattern)[[1]])\n\n# A more complex string\ntmpstr = \"The first number is: 32.  342 342.1   -3234e-10 \n3234e-1 Another one is: 32.1. Here's a number in scientific \nformat, 0.3523e10, and another, 0.3523e-10, and a negative, \n-313.1\"\n#pattern = \n\"(\\\\d)+|(-\\\\d)+|(\\\\d+\\\\.\\\\d+)|(-\\\\d+\\\\.\\\\d+)|(\\\\d+.\\\\d+e\\\\d+)|(\\\\d+\\\\.\\\\d+e-\\\\d+)|(-\\\\d+.\\\\d+e\\\\d+)|(-\\\\d+\\\\.\\\\d+e-\\\\d+)\"\nas.numeric(str_extract_all(tmpstr,pattern)[[1]])\n\n\n\nCheers!\nNick\n\n\nPS: A function version:\n\n\n# Extract numbers \/ get numbers \/ get all numbers from a \ntext string\ngetnums &lt;- function(tmpstr)\n\t{\n\t# Example string\n\t# tmpstr = \"The first number is: 32.  342 342.1   -3234e-10 \n  3234e-1 Another one is: 32.1. Here's a number in \nscientific format, 0.3523e10, and another, 0.3523e-10, and a \nnegative, -313.1\"\n\t\n\tlibrary(stringr)\n\t\n# \tpatternslist = NULL\n# \tp=0\n# \tpatternslist[[(p=p+1)]] = \"(\\\\d+)\"\t\t\t\t# positive integer\n# \tpatternslist[[(p=p+1)]] = \"(-\\\\d+)\"\t\t\t\t# negative integer\n# \tpatternslist[[(p=p+1)]] = \"(\\\\d+\\\\.\\\\d+)\"\t\t# positive float\n# \tpatternslist[[(p=p+1)]] = \"(\\\\d+\\\\.\\\\d+e\\\\d+)\"\t# positive \nfloat, scientific w. positive power\n# \tpatternslist[[(p=p+1)]] = \"(\\\\d+\\\\.\\\\d+e-\\\\d+)\" # \npositive float, scientific w. negative power\n# \tpatternslist[[(p=p+1)]] = \"(-\\\\d+\\\\.\\\\d+)\"\t\t# negative float\n# \tpatternslist[[(p=p+1)]] = \"(-\\\\d+\\\\.\\\\d+e\\\\d+)\"\t# \nnegative float, scientific w. positive power\n# \tpatternslist[[(p=p+1)]] = \"(-\\\\d+\\\\.\\\\d+e-\\\\d+)\"# \nnegative float, scientific w. negative power\n# \t\n# \tpatternslist[[(p=p+1)]] = \"(\\\\d+e\\\\d+)\"\t\t\t# positive int, \nscientific w. positive power\n# \tpatternslist[[(p=p+1)]] = \"(\\\\d+e-\\\\d+)\" \t\t# positive \nint, scientific w. negative power\n# \tpatternslist[[(p=p+1)]] = \"(-\\\\d+e\\\\d+)\"\t\t# negative int, \nscientific w. positive power\n# \tpatternslist[[(p=p+1)]] = \"(-\\\\d+e-\\\\d+)\"\t\t# negative \nint, scientific w. negative power\n# \t\n# \tpattern = paste(patternslist, collapse=\"|\", sep=\"\")\n\n\t# set up the pattern\n\tpattern = \n\"(\\\\d+)|(-\\\\d+)|(\\\\d+\\\\.\\\\d+)|(\\\\d+\\\\.\\\\d+e\\\\d+)|(\\\\d+\\\\.\\\\d+e-\\\\d+)|(-\\\\d+\\\\.\\\\d+)|(-\\\\d+\\\\.\\\\d+e\\\\d+)|(-\\\\d+\\\\.\\\\d+e-\\\\d+)|(\\\\d+e\\\\d+)|(\\\\d+e-\\\\d+)|(-\\\\d+e\\\\d+)|(-\\\\d+e-\\\\d+)\"\n\t\n\t# Get the numbers\n\tnums_from_tmpstr = \nas.numeric(str_extract_all(tmpstr,pattern)[[1]])\n\n\t# Return them\n\treturn(nums_from_tmpstr)\n\t}\n\n\n\n\n\n\n\n\n\n\n\n\nOn 6\/15\/13 10:46 PM, arun wrote:\n&gt;<i>\n<\/i>&gt;<i>\n<\/i>&gt;<i> HI,\n<\/i>&gt;<i> One way would be:\n<\/i>&gt;<i>\n<\/i>&gt;<i> library(stringr)\n<\/i>&gt;<i> tmpstr = \"The first number is: 32.  Another one is: 32.1.\n<\/i>&gt;<i> Here's a number in scientific format, 0.3523e10, and\n<\/i>&gt;<i> another, 0.3523e-10, and a negative, -313.1\"\n<\/i>&gt;<i> pattern&lt;- \"(\\\\d)+|(\\\\d+\\\\.\\\\d+)|(-\\\\d+\\\\.\\\\d+)|(\\\\d+.\\\\d+e\\\\d+)|(\\\\d+\\\\.\\\\d+e-\\\\d+)\"\n<\/i>&gt;<i> str_extract_all(tmpstr,pattern)[[1]]\n<\/i>&gt;<i> #[1] \"32\"         \"32.1\"       \"0.3523e10\"  \"0.3523e-10\" \"-313.1\"\n<\/i>&gt;<i>   as.numeric(str_extract_all(tmpstr,pattern)[[1]])\n<\/i>&gt;<i> A.K.\n<\/i>&gt;<i>\n<\/i>&gt;<i>\n<\/i>&gt;<i>\n<\/i>&gt;<i> ----- Original Message -----\n<\/i>&gt;<i> From: Nick Matzke &lt;<a href=\"https:\/\/stat.ethz.ch\/mailman\/listinfo\/r-help\">matzke at berkeley.edu<\/a>&gt;\n<\/i>&gt;<i> To: <a href=\"https:\/\/stat.ethz.ch\/mailman\/listinfo\/r-help\">R-help at r-project.org<\/a>\n<\/i>&gt;<i> Cc:\n<\/i>&gt;<i> Sent: Sunday, June 16, 2013 1:06 AM\n<\/i>&gt;<i> Subject: [R] extract all numbers from a string\n<\/i>&gt;<i>\n<\/i>&gt;<i> Hi all,\n<\/i>&gt;<i>\n<\/i>&gt;<i> I have been beating my head against this problem for a bit,\n<\/i>&gt;<i> but I can't figure it out.\n<\/i>&gt;<i>\n<\/i>&gt;<i> I have a series of strings of variable length, and each will\n<\/i>&gt;<i> have one or more numbers, of varying format.  E.g., I might\n<\/i>&gt;<i> have:\n<\/i>&gt;<i>\n<\/i>&gt;<i>\n<\/i>&gt;<i> tmpstr = \"The first number is: 32.  Another one is: 32.1.\n<\/i>&gt;<i> Here's a number in scientific format, 0.3523e10, and\n<\/i>&gt;<i> another, 0.3523e-10, and a negative, -313.1\"\n<\/i>&gt;<i>\n<\/i>&gt;<i> How could I get R to just give me a list of numerics\n<\/i>&gt;<i> containing the numbers therein?\n<\/i>&gt;<i>\n<\/i>&gt;<i> Thanks very much to the regexp wizards!\n<\/i>&gt;<i>\n<\/i>&gt;<i> Cheers,\n<\/i>&gt;<i> Nick\n<\/i>&gt;<i>\n<\/i>&gt;<i>\n<\/i>&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Thanks *VERY* much, this is great! I realized a few more cases, I think I&#8217;ve got something that covers all the possibilities now: library(stringr) tmpstr&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-636","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=636"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/636\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}