Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #221 Remove all non-digits characters

Create string t from string s, keeping only digit characters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

my $t = $s;
$t =~ s/\D+//g;
(require '[clojure.string :as str])
(let [s "1a22b3c4de5f6"
      t (str/replace s #"[^\d]" "")]
  (println t))
import "regexp"
re := regexp.MustCompile("[^\\d]")
t := re.ReplaceAllLiteralString(s, "")
t = filter (`elem` ['0'..'9']) s
t = s.replace(/[^\d]/gm,"");
RegExpr
t := ReplaceRegExpr('[^\d]', s, ''); 
for i := 1 to length(s) do
    if s[i] in ['0'..'9'] then
        t := t + s[i];
import re
t = re.sub(r"\D", "", s)
t = s.delete("^0-9")

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms