NoPaste

replexp/main.go

von paedubucher
SNIPPET_DESC:
REPL für reguläre Ausdrücke in Go
SNIPPET_CREATION_TIME:
19.06.2022 16:52:46
SNIPPET_PRUNE_TIME:
Unendlich

SNIPPET_TEXT:
  1. package main
  2.  
  3. import (
  4.         "bufio"
  5.         "fmt"
  6.         "io"
  7.         "os"
  8.         "regexp"
  9. )
  10.  
  11. func main() {
  12.         if len(os.Args) < 2 {
  13.                 fmt.Fprintf(os.Stderr, "usage: %s [regexp]\n", os.Args[0])
  14.                 os.Exit(1)
  15.         }
  16.         pattern := os.Args[1]
  17.  
  18.         input := bufio.NewReader(os.Stdin)
  19.         var line []byte
  20.         var err error
  21.         for ; err != io.EOF; line, err = input.ReadBytes('\n') {
  22.                 if len(line) == 0 {
  23.                         continue
  24.                 }
  25.                 if ok, _ := regexp.Match(pattern, line); ok {
  26.                         fmt.Print(string(line))
  27.                 }
  28.         }
  29. }

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN