EDIT: If you're using Lisp, I have given some guidelines at the bottom in counting bytes.
Objective: Make the shortest function that splits a string at non-digits and returns an array consisting of only digits in each string, without the use of any regular expressions. Leading zeroes are to be included in each string.
Current Standings (separated in categories):
- C/C++/C#/Java: 68 (C) ....
- GolfScript/APL/J: 13 (APL)
- All others: 17 (Bash, uses
tr
), 24 (Ruby)
Rules:
The format must be as a function with a single string argument. Up to two additional arguments may be added if necessary for the proper return of the array (e.g. sh/csh/DOS Batch needs an extra variable reference to return, etc.).
The primary function declaration doesn't count, and nor does importing other standard libraries.
#include
s,import
s, andusing
s don't count. Everything else does. This does include#define
s and helper functions. Sorry for the confusion. Refer to this as a helpful guide as to what does/does not count (written in C-style syntax)// doesn't count toward total, may be omitted unless // non-obvious, like half of Java's standard library. #include <stdio.h> import some.builtin.Class // doesn't count, see above #define printf p // counts towards total /* Any other preprocessor directives, etc. count. */ int i = 0; // counts someFunction(); // counts char[][] myMainSplitFunction(char[][] array) { // doesn't count // Everything in here counts return returnArray; // Even this counts. } // doesn't count /* Everything in here counts, including the declaration */ char[][] someHelperFunction(char[] string) { // stuff } // even this counts
The output must be a string array or similar (Array lists in Java and similar are acceptable). Examples of accepted output:
String[]
,char[][]
,Array<String>
,List<String>
, andArray
(object).The array must contain only contain variable-length string primitives or string objects. No empty strings should be present in the return, with the exception below. Note: the strings are to contain a string of consecutive matches, such as the example input and output below.
If there are no matches, then the function body should return
null
, an empty array/list, or an array/list containing an empty string.No external libraries allowed.
DOS line endings count as one byte, not two (already covered in meta, but needs to be emphasized)
And the biggest rule here: no regular expressions allowed.
This is a code-golf question, so shortest code wins. Good luck!
And here are some example inputs and outputs (with C-style escapes):
Input: "abc123def456" Output: ["123", "456"] Input: "aitew034snk582:3c" Output: ["034", "582", "3"] Input: "as5493tax54\\[email protected]" Output: ["5493", "54", "430", "52", "9"] Input: "sasprs]tore\"re\\forz" Output: null, [], [""], or similar
Please put how many bytes used by your answers, and as always, happy golfing!
Guidelines for Lisp
Here's what does and doesn't count in Lisp dialects:
;;; Option 1All other lambdas fully count towards the byte count.(defun extract-strings (a b) ; Doesn't count (stuff) ;;; Everything in here counts ) ; Doesn't count
;;; Option 2
(defun extract-strings (string &aux (start 0) (end 0)) ; Doesn't count (stuff) ;;; Everything in here counts ) ; Doesn't count.