-
-
Save hyuki/28fa9d9bcecaa02117a1f1b5f9358f40 to your computer and use it in GitHub Desktop.
regexp.MatchString で「あ」で始まる行のみ表示する。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"log" | |
"regexp" | |
) | |
func check(err error) { | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func main() { | |
if len(os.Args) != 3 { | |
fmt.Printf("Usage: %s input-filename output-filename\n", os.Args[0]) | |
os.Exit(1) | |
} | |
infilename := os.Args[1] | |
outfilename := os.Args[2] | |
fmt.Printf("input-filename = %s\n", infilename) | |
fmt.Printf("output-filename = %s\n", outfilename) | |
infile, err := os.Open(infilename) | |
check(err) | |
outfile, err := os.Create(outfilename) | |
check(err) | |
linenumber := 0 | |
scanner := bufio.NewScanner(infile) | |
for scanner.Scan() { | |
matched, err := regexp.MatchString("^あ", scanner.Text()) | |
check(err) | |
if matched { | |
fmt.Fprintf(outfile, "%d: %s\n", linenumber, scanner.Text()) | |
} | |
linenumber++ | |
} | |
check(scanner.Err()) | |
check(infile.Close()) | |
check(outfile.Close()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ゆく河の流れは絶えずして、しかももとの水にあらず。 | |
淀みに浮かぶうたかたは、かつ消えかつ結びて、久しくとどまりたるためしなし。 | |
世の中にある人とすみかと、またかくのごとし。 | |
たましきの都のうちに、棟を並べ、甍を争へる、高き、卑しき、人のすまひは、 | |
世々を経て尽きせぬものなれど、これをまことかと尋ぬれば、昔ありし家はまれなり。 | |
あるいは去年焼けて今年作れり。あるいは大家滅びて小家となる。住む人もこれに同じ。 | |
所も変はらず、人も多かれど、いにしへ見し人は、二、三十人が中に、 | |
わづかにひとりふたりなり。朝に死に、夕べに生まるるならひ、 | |
ただ水のあわにぞ似たりける。知らず、生まれ死ぬる人、 | |
いづかたより来たりて、いづかたへか去る。また知らず、仮の宿り、 | |
たがためにか心を悩まし、何によりてか目を喜ばしむる。 | |
その、あるじとすみかと、無常を争ふさま、いはば朝顔の露に異ならず。 | |
あるいは露落ちて花残れり。残るといへども朝日に枯れぬ。 | |
あるいは花しぼみて露なほ消えず。消えずといへども夕べを待つことなし。 |
Author
hyuki
commented
Apr 9, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment