Go 정규표현식

1 개요[ | ]

Go Regular Expressions
Go 정규표현식
package main

import "fmt"
import "regexp"

func main() {
	match, _ := regexp.MatchString("p([a-z]+)ch", "peach")
	fmt.Println(match) // true

	r, _ := regexp.Compile("p([a-z]+)ch")
	fmt.Println(r.MatchString("peach")) // true
}
package main

import "fmt"
import "regexp"

func main() {
	r, _ := regexp.Compile("p([a-z]+)ch") 

	fmt.Println(r.FindString("peach punch")) // peach
	fmt.Println("idx:", r.FindStringIndex("peach punch")) // idx: [0 5]

	fmt.Println(r.FindStringSubmatch("peach punch")) // [peach ea]
	fmt.Println(r.FindStringSubmatchIndex("peach punch")) // [0 5 1 3]
}
package main

import "fmt"
import "regexp"

func main() {
	r, _ := regexp.Compile("p([a-z]+)ch")

	fmt.Println(r.FindAllString("peach punch pinch", -1))                      // [peach punch pinch]
	fmt.Println("all:", r.FindAllStringSubmatchIndex("peach punch pinch", -1)) // all: [[0 5 1 3] [6 11 7 9] [12 17 13 15]]
	fmt.Println(r.FindAllString("peach punch pinch", 2))                       // [peach punch]

	fmt.Println(r.Match([]byte("peach"))) // true
}
package main

import "fmt"
import "regexp"

func main() {
	r := regexp.MustCompile("p([a-z]+)ch")
	fmt.Println("regexp:", r)                             // regexp: p([a-z]+)ch
	fmt.Println(r.ReplaceAllString("a peach", "<fruit>")) // a <fruit>
}
package main

import "fmt"
import "regexp"
import "bytes"

func main() {
	r := regexp.MustCompile("p([a-z]+)ch")
	in := []byte("a peach")
	out := r.ReplaceAllFunc(in, bytes.ToUpper)
	fmt.Println(string(out)) // a PEACH
}

2 같이 보기[ | ]

3 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}