首页 > 代码库 > 第一个racket程序:获得字符串中的数字
第一个racket程序:获得字符串中的数字
#! /usr/bin/env racket #lang racket #| NAME: getModelNumber.rkt This program is used to get model number based on the input model file name. USAGE: ./getModelNumber.rkt modelfilename EXAMPLE: ./getModelNumber.rkt gitst22.mat OUTPUT: 22 |# ;; deal with the arguments (define verbose? (make-parameter #f)) (define fileformat? (make-parameter #f)) (define argslist (command-line #:usage-help "Get model number based on the filename." #:once-each [("-v" "--verbose") "Verbose mode" (verbose? #t)] #:args (filename [format ".mat"]) (list filename format))) ;; define indicator (define number -1) (define filename (list-ref argslist 0)) (define format (list-ref argslist 1)) ;; check the filename format is legal. (if (regexp-match? (string-append format "$") filename) (fileformat? #t) (when (verbose?) (display "Filename must be end format of .mat \n"))); ;; get the model number (define getNumber (lambda (filename) (regexp-match #px"\\d+" filename))) (when (and (getNumber filename) (fileformat?)) (set! number (string->number (list-ref (getNumber filename) 0)))) number ; return number value.
运行如下:
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。