Categorygithub.com/agext/regexp
modulepackage
1.3.0
Repository: https://github.com/agext/regexp.git
Documentation: pkg.go.dev

# README

Extended regexp package for Go (golang)

Release GoDoc  Build Status Coverage Status Go Report Card

This is an extension of the standard Go package with the same name. It is a modified copy that can be used as a simple drop-in replacement, being 100% backwards-compatible with the standard package. It adds new methods to the Regexp type to allow for more efficient processing of submatches.

Maturity

v1.3.0 Stable: Guaranteed no breaking changes to the API in future v1.x releases. No known bugs or performance issues introduced by the added code. Probably safe to use in production, though provided on "AS IS" basis.

This package is being actively maintained. If you encounter any problems or have any suggestions for improvement, please open an issue. Pull requests are welcome.

Note on failing test for older versions (and possibly tip)

The TestFoldConstants in syntax/parse_test.go depends on the standard unicode/utf8 package. This test fails when this package is used with a Go version in which the unicode/utf8 package handles different folding ranges (new ranges are added from time to time, as unicode/utf8 is refined). The behavior of agext/regexp will be the one you expect from your Go version, based on its unicode/utf8 package, so it is safe to ignore this failing test.

Overview

This package provides the following additional methods on the Regexp type:

FindNamed and FindStringNamed

These methods work like the standard FindSubmatch and FindStringSubmatch, except they return a map {subexpName: subMatch...}, with elements only for the named subexpressions, plus the whole match with an empty string key. This ensures the returned map has at least one element when there was a match, even if the pattern has no named subexpressions, and is consistent with the standard 'Submatch' methods returning the whole match as the zeroth element.

FindAllNamed and FindAllStringNamed

These are the 'All' version of FindNamed and FindStringNamed. They return a slice of all successive matches of the expression, as defined by the 'All' description in the package comment. The slice elements are maps with the same semantics as the returns of FindNamed and FindStringNamed, respectively.

ReplaceAllSubmatchFunc and ReplaceAllStringSubmatchFunc

These methods work like the standard ReplaceAllFunc and ReplaceAllStringFunc, but the replace function receives a slice containing the match and all submatches (like the return of 'FindSubmatch' methods), instead of just the match.

ReplaceAllNamedFunc and ReplaceAllStringNamedFunc

These methods work like the standard ReplaceAllFunc and ReplaceAllStringFunc, but the replace function receives a map containing the match and all named submatches (like the return of 'FindNamed' methods), instead of just the match.

Installation

go get github.com/agext/regexp

License

Package regexp is released under the Apache 2.0 license. See the LICENSE file for details.

# Packages

Package syntax parses regular expressions into parse trees and compiles parse trees into programs.

# Functions

Compile parses a regular expression and returns, if successful, a Regexp object that can be used to match against text.
CompilePOSIX is like Compile but restricts the regular expression to POSIX ERE (egrep) syntax and changes the match semantics to leftmost-longest.
Match reports whether the byte slice b contains any match of the regular expression pattern.
MatchReader reports whether the text returned by the RuneReader contains any match of the regular expression pattern.
MatchString reports whether the string s contains any match of the regular expression pattern.
MustCompile is like Compile but panics if the expression cannot be parsed.
MustCompilePOSIX is like CompilePOSIX but panics if the expression cannot be parsed.
QuoteMeta returns a string that escapes all regular expression metacharacters inside the argument text; the returned string is a regular expression matching the literal text.

# Structs

Regexp is the representation of a compiled regular expression.