go - Regex match does weird things for me -
I am trying to create a simple command line, date entry function.
I get strange behavior while matching regexp pattern with golfing: The correct pattern makes the Mac lie but spoils with my loop, while the wrong format just returns false. Function that generates error:
func ReadDate (field name string) (value string) {var validID = regexp.MustCompile (`^ \ d \ d \ d \ d \ s ({Value = "" Fmt.printf ("% s - example for January 2014) for January | February | March | Apr | May | Jun | July | Aug | Sep | Oct | Dec | Days) \ s \ d \ d $ Fmt.Scanf ("% s \ n", and value) if value == "" {break // empty value is fine for input}, fmt.printf ("validid% v \ n" ", ValidID.MatchString (value)) if validID.MatchString (value) {break} else {fmt.Printf (" Invalid entry, reprint When I run it, I get the following: Dates - example 2014 January 01 : X valid false date - e.g. 2014 January 01: X valid false date - eg. January 01, 2014 January 01 Validated wrong date - e.g. January 01, 2014: Validated wrong date - e.g. January 01, 2014: Validated wrong date - e.g. 2014 January 01:
Notice how the last entry with the right pattern runs twice through the infinite loop and then closes. Why can any one think?
Version 1.2 Linux / 386
The problem is that Scanf < / Code> returns the data before the entire line I have modified my code to use the bufio.Scanner and os.Stdin , and it works now: Func ReadDate (Field Name String) (Value String) {var validID = regexp.MustCompile (`^ \ d \") D \ d \ d \ s (Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Days] S \ d \ d $ `) fmt.printf ("% s - example 2014 January 01: ", fieldname) scanner: = bufio.NewScanner (Os.Stdin) scanner.Scan () {value = scanner.Text () fmt .printf ("value read: ''% s' \ n", value) if value == "" {break // empty value is right for input} fmt.printf ("validid% v \ n", validID.matchString (Value)) if validID.MatchString (value) {break} else {fmt.Printf ("invalid entry, try again .. \ n")} Fmt.Printf ("% s - example 2014 January 01:", field name }} Return} function main () {fmt.Println ("back", reddet ("af")}
Comments
Post a Comment