Home > Backend Development > Golang > Streaming regex scanner — regexpscanner

Streaming regex scanner — regexpscanner

Patricia Arquette
Release: 2024-12-06 16:10:16
Original
742 people have browsed it

Streaming regex scanner — regexpscanner

Go's regexp module falls short with stream processing-- nearly all methods require a string or []byte. The regexpscanner module makes it easy to extract tokens that match regular expression patterns.

https://pkg.go.dev/github.com/tonymet/regexpscanner

Install Module

go get github.com/tonymet/regexpscanner@latest
Copy after login

Example Usage

use ProcessTokens when a simple callback-based stream tokenizer is needed .
ProcessTokens calls handler(string) for each matching token from the Scanner.

package main

import (
    "fmt"
    "regexp"
    "strings"

    rs "github.com/tonymet/regexpscanner"
)

func main() {
    rs.ProcessTokens(
        strings.NewReader("<html><body><p>Welcome to My Website</p></body></html>"),
        regexp.MustCompile(`</?[a-z]+>`),
        func(text string) {
            fmt.Println(text)
        })
}
Copy after login

Output

<html>
<body>
<p>
</p>
</body>
</html>
Copy after login

Give it a try and see the Go Module Page for more examples

The above is the detailed content of Streaming regex scanner — regexpscanner. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template