Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String[]
- {⍵⊂⍨⍵∊∊⍕¨⍳10}
- {⍵⊂⍨⍵∊∊⍕¨⍳10}
- {⍵⊂⍨⍵∊∊⍕¨⍳10}
- { ⍳10} make an array of naturals from 1 to 10
- ⍕¨ convert each number into a string
- ∊ concatenate the strings into one (it doesn't matter that there are two 1s)
- ⍵∊ test which chars from the argument are contained in the digit string
- ⍵⊂⍨ use it to perform a partitioned enclose, which splits the string as needed
- {⍵⊂⍨⍵∊∊⍕¨⍳10} 'ab5c0x'
- 5 0
- {⍵⊂⍨⍵∊∊⍕¨⍳10} 'z526ks4f.;8]p'
- 526 4 8
- {q,⍵,q←'"'}¨ {⍵⊂⍨⍵∊∊⍕¨⍳10} 'ab5c0x'
- "5" "0"
- {q,⍵,q←'"'}¨ {⍵⊂⍨⍵∊∊⍕¨⍳10} 'z526ks4f.;8]p'
- "526" "4" "8"
- f()(echo ${1//+([!0-9])/ })
- $ a=($(f "ab5c0x")); echo ${a[@]}
- 5 0
- $ a=($(f "z526ks4f.;8]p")); echo ${a[@]}
- 526 4 8
- $
- >>> sample=["abc123def456","aitew034snk582:3c","as5493tax54\430-52@g9.fc","sasprs]tore"re\forz"]
- >>> [f(data) for data in sample]
- [['123', '456'], ['034', '582', '3'], ['5493', '54', '430', '52', '9'], []]
- function split() {
- tr -c 0-9 <<E
- $1
- E
- }
- $ for N in $(split 'abc123def456'); do echo $N; done
- 123
- 456
- function split() (tr -c 0-9 <<<$1)
- a=(`split "abc123def456"`); echo ${a[@]}
- StringCases[#,DigitCharacter..]&
- inps ={"abc123def456", "aitew034snk582:3c", "as5493tax54\430-52@g9.fc",
- "sasprs]tore"re\forz"}
- StringCases[#,DigitCharacter..]&/@inps
- {{"123", "456"},
- {"034", "582", "3"},
- {"5493", "54", "430", "52", "9"},
- {}
- }
- StringCases[#, RegularExpression["[0-9]+"]] &
- f := [:s|s asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch|ch isDigit not]]
- [ 123 456 ]
- [ 034 582 3 ]
- [ 5493 54 430 52 9 ]
- [ ]
- Function f(i)
- For x=1 To Len(i)
- c=Mid(i,x,1)
- If Not IsNumeric(c) Then
- Mid(i,x,1)=" "
- End If
- Next
- Do
- l=Len(i)
- i=Replace(i," "," ")
- l=l-Len(i)
- Loop Until l=0
- f=Split(Trim(i)," ")
- End Function
- Input: "ab5c0x"
- Output: 5,0
- Input: "z526ks4f.;8]p"
- Output: 526,4,8
- function f(a){
- a+=".",b="",c=[];for(i in a)b=+a[i]+1?b+a[i]:b?(c.push(b),""):b;return c
- }
- a+=".",b="",c=[]; //add '.' to input so we dont have to check if it ends in a digit
- for(i in a)
- b=+a[i]+1? //check if digit, add to string if it is
- b+a[i]:
- b? //if it wasnt a digit and b contains digits push it
- (c.push(b),""): //into the array c and clear b
- b; //else give me b back
- return c
- console.log(f("abc123def456"));
- console.log(f("aitew034snk582:3c"));
- console.log(f("as5493tax54\430-52@g9.fc"));
- console.log(f("sasprs]tore"re\forz"));
- ["123", "456"]
- ["034", "582", "3"]
- ["5493", "54", "430", "52", "9"]
- []
- f=function(x){
- s=strsplit(x,"",T)[[1]]
- i=s%in%0:9
- split(s,c(0,cumsum(!!diff(i))))[c(i[1],!i[1])]
- }
- > f("abc123def456")
- $`1`
- [1] "1" "2" "3"
- $`3`
- [1] "4" "5" "6"
- > f("aitew034snk582:3c")
- $`1`
- [1] "0" "3" "4"
- $`3`
- [1] "5" "8" "2"
- $`5`
- [1] "3"
- > f("as5493tax54\430-52@g9.fc")
- $`1`
- [1] "5" "4" "9" "3"
- $`3`
- [1] "5" "4"
- $`5`
- [1] "4" "3" "0"
- $`7`
- [1] "5" "2"
- $`9`
- [1] "9"
- > f("sasprs]tore"re\forz")
- $<NA>
- NULL
- function f($a){
- $i=0;while($i<strlen($a)){!is_numeric($a[$i])&&$a[$i]='-';$i++;}return array_filter(explode('-',$a),function($v){return!empty($v);});
- }
- static string[] SplitAtNonDigits(string s)
- {
- return new string(s.Select(c=>47<c&c<58?c:',').ToArray()).Split(new[]{','},(StringSplitOptions)1);
- }
- function n(s){
- var r=[];s.split('').reduce(function(p,c){if(!isNaN(parseInt(c))){if(p)r.push([]);r[r.length-1].push(c);return 0;}return 1;},1);return r;
- }
- function n(s) {
- var r = [];
- s.split('').reduce(function (p, c) {
- if (!isNaN(parseInt(c))) {
- if (p) {
- r.push([]);
- }
- r[r.length - 1].push(c);
- return 0;
- }
- return 1;
- }, 1);
- return r;
- }
- f=->s{s.tr("
- -/:-~",' ').split}
- def find_digits(_input_):
- a,b = [], ""
- for i in list(_input_):
- if i.isdigit(): b += i
- else:
- if b != "": a.append(b)
- b = ""
- if b != "": a.append(b)
- return a
- def f(s, o=[], c=""):
- for i in s:
- try:int(i);c+=i
- except:o+=[c];c=""
- return [i for i in o+[c] if i]
- assert f("abc123def456") == ["123", "456"]
- assert f("aitew034snk582:3c") == ["034", "582", "3"]
- assert f("as5493tax54\430-52@g9.fc") == ["5493", "54", "430", "52", "9"]
- assert f("sasprs]tore"re\forz") == []
- function f($x) {
- // Only the following line counts:
- for($h=$i=0;sscanf(substr("a$x",$h+=$i),"%[^0-9]%[0-9]%n",$j,$s,$i)>1;)$a[]=$s;return@$a;
- }
- php > echo json_encode(f("abc123def456")), "n";
- ["123","456"]
- php > echo json_encode(f("aitew034snk582:3c")), "n";
- ["034","582","3"]
- php > echo json_encode(f("as5493tax54\430-52@g9.fc")), "n";
- ["5493","54","430","52","9"]
- php > echo json_encode(f("sasprs]tore"re\forz")), "n";
- null
- {-# LANGUAGE OverloadedStrings #-}
- import Data.Char (isDigit)
- import Data.Text (split)
- f=filter(/="").split(not.isDigit)
- (defun extract-numeric-substrings (string &aux (start 0) (end 0) (result (make-array 0 :adjustable t :fill-pointer 0)))
- (loop
- (unless (and end (setq start (position-if #'digit-char-p string :start end)))
- (return result))
- (setq end (position-if (complement #'digit-char-p) string :start (1+ start)))
- (vector-push-extend (subseq string start end) result)))
- (extract-numeric-substrings "abc123def456")
- #("123" "456")
- (extract-numeric-substrings "aitew034snk582:3c")
- #("034" "582" "3")
- (extract-numeric-substrings "as5493tax54\430-52@g9.fc")
- #("5493" "54" "430" "52" "9")
- (extract-numeric-substrings "sasprs]tore"re\forz")
- #()
- (defun extract-numeric-substrings (string
- &aux (start 0) (end 0)
- (result (make-array 0 :adjustable t :fill-pointer 0))
- (x (loop
- (unless (and end (setq start (position-if #'digit-char-p string :start end)))
- (return result))
- (setq end (position-if (complement #'digit-char-p) string :start (1+ start)))
- (vector-push-extend (subseq string start end) result))))
- x)
- (LOOP(UNLESS(AND END(SETQ START(POSITION-IF #'DIGIT-CHAR-P STRING :START END)))(RETURN RESULT))(SETQ END(POSITION-IF(COMPLEMENT #'DIGIT-CHAR-P)STRING :START(1+ START)))(VECTOR-PUSH-EXTEND(SUBSEQ STRING START END)RESULT))
- (defun extract-numeric-substrings (s &aux (b 0) (e 0) (r (make-array 0 :fill-pointer 0)))
- (loop
- with d = #'digit-char-p
- while (and e (setq b (position-if d s :start e)))
- finally (return r)
- do
- (setq e (position-if-not d s :start (1+ b)))
- (vector-push-extend (subseq s b e) r)))
- (LOOP WITH D = #'DIGIT-CHAR-P WHILE(AND E(SETQ B(POSITION-IF D S :START E)))FINALLY(RETURN R)DO(SETQ E(POSITION-IF-NOT D S :START(1+ B)))(VECTOR-PUSH-EXTEND(SUBSEQ S B E)R))
- Function t(s)
- Dim o()
- For Each c In Split(StrConv(s,64),Chr(0))
- d=IsNumeric(c)
- If b And d Then
- n=n&c
- ElseIf d Then:ReDim Preserve o(l):b=1:n=c
- ElseIf b Then:b=0:o(l)=n:l=l+1:End If:Next:t=o
- End Function
- remove-each n s: split s complement charset"0123456789"[empty? n]s
- f: func [s] [
- remove-each n s: split s complement charset "0123456789" [empty? n]
- s
- ]
- >> f "abc123def456"
- == ["123" "456"]
- >> f "aitew035snk582:3c"
- == ["035" "582" "3"]
- >> f "as5493tax54\430-52@g9.fc"
- == ["5493" "54" "430" "52" "9"]
- >> f {sasprs]torer"re\forz}
- == []
- function nums(s) {
- s+=l='length';r=[''];for(k=i=0;c=s[i];i++)r[k]+=+c+1?c:r[k+=!!r[k][l]]='';
- r[l]--;return r
- }
- function nums(s) {
- var i, e, r, c, k;
- k = 0;
- s+='x'; // ensure the input does not end with a digit
- r=[''];
- for (i=0;i<s.length;i++) {
- c=s[i];
- if (+c+1) { // if the current character is a digit, append it to the last entry
- r[k] += c;
- }
- else { // otherwise, add a new entry if the last entry is not blank
- k+=!!r[k].length;
- r[k] = '';
- }
- }
- r.length--; // strip the last entry, known to be blank
- return r;
- }
- x
- ## [1] "wNEKbS0q7hAXRVCF6I4S" "DpqW50YfaDMURB8micYd" "gwSuYstMGi8H7gDAoHJu"
- require(stringi)
- stri_split_charclass(x,"\P{N}",o=T)
- ## [[1]]
- ## [1] "0" "7" "6" "4"
- ## [[2]]
- ## [1] "50" "8"
- ## [[3]]
- ## [1] "8" "7"
- <?php
- $a = function($s) {
- foreach(str_split($s)as$c)$b[]=is_numeric($c)?$c:".";return array_filter(explode('.',implode($b)));
- };
- var_dump($a("abc123def456"));
- var_dump($a("aitew034snk582:3c"));
- var_dump($a("as5493tax54\430-52@g9.fc"));
- var_dump($a("sasprs]tore"re\forz"));
- array(2) {
- [3]=>
- string(3) "123"
- [6]=>
- string(3) "456"
- }
- array(3) {
- [5]=>
- string(3) "034"
- [8]=>
- string(3) "582"
- [9]=>
- string(1) "3"
- }
- array(5) {
- [2]=>
- string(4) "5493"
- [5]=>
- string(2) "54"
- [6]=>
- string(3) "430"
- [7]=>
- string(2) "52"
- [9]=>
- string(1) "9"
- }
- array(0) {
- }
- function n(x){
- y=[],i=0,z=t=''
- while(z=x[i++])t=!isNaN(z)?t+z:t&&y.push(t)?'':t
- if(t)y.push(t)
- return y
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement