Open file in racket and use regex on said file to print matches -
i have been trying use regular expressions in racket on text file full of random words separated end of line character \n. i'm trying read in file string or list (whichever easiest , intuitive) , use regex print words in file of length 6 not contain letter (in case letter t). below can see how read in file not sure how use resulting list because of lack of variables. can see below try test regex that's true outcome #f when want words grumpy , foobar returned excluding stumpy.
#lang racket (require 2htdp/batch-io) (require racket/match) ;(file->string "words.txt");;reads in file string ;(file->list "words.txt);; reads in file list (define (listmatches) (regexp-match #rx"\b[^<t> | ^<t> | ^<\n>]{<6>}\b" "grumpy\nstumpy\nfoobar" ) ) i new racket , love input, useful links, , other help.
i not use regex @ all, rather use for/list, in combination string-length , string-countains? solve problem. overall solution looks this:
(call-with-input-file* "words.txt" (lambda (f) (for/list ([i (in-lines f)] #:when (and (= (string-length i) 6) (not (string-contains? "t")))) i))) the use of call-with-input-file* takes procedure, , in case binds f open file. way not need close file ourselves when done it.
finally, string-contains? added relatively racket. , if need support older versions of racket, can use regexp-match search "t", easier.
Comments
Post a Comment