regexp

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

基础语法

  • 转义 \ , 只可将元字符\ * | ( )转义
  • 连接 abc
  • |
  • 闭包 * 只可出现在一个字母表字符)之后,使这个正则表达式重复0到任意次

高级语法 (这些语法均只可出现在一个字母表字符)之后)

  • ? 被修饰的正则表达式出现0或1次
  • + 被修饰的正则表达式出现至少1次
  • {n} 被修饰的正则表达式一定是出现n次
  • {n-m} 被修饰的正则表达式出现n-m次

package main

import (
	"fmt"
	"github.com/howz97/algorithm/regexp"
)

const (
	Match   = 0
	UnMatch = 1
)

var match = map[string][2][]string{
	`(1(\\|\(|c)*2)`:     {Match: {`12`, `1c((\\2`}, UnMatch: {`1*2`}}, // \
	`(1(a|b|c|d)+2)`:     {Match: {`1a2`, `1aabbdcdc2`}, UnMatch: {`12`}},
	`(1a+2)`:             {Match: {`1a2`, `1aaaaa2`}, UnMatch: {`12`}}, // +
	`(1(a|b|c|d)?2)`:     {Match: {`12`, `1d2`}, UnMatch: {`1bc2`}},    // |
	`(1a?2)`:             {Match: {`12`, `1a2`}, UnMatch: {`1aa2`}},
	`(1(a|b|c|d){3}2)`:   {Match: {`1abc2`}, UnMatch: {`1ab2`, `1abcd2`}}, // {n}
	`(1a{3}2)`:           {Match: {`1aaa2`}, UnMatch: {`1aa2`, `1aaaa2`}},
	`(1(a|b|c|豪){0-3}2)`: {Match: {`12`, `1ab豪2`}, UnMatch: {`1abc豪2`}}, // // {n-m}
	`(1a{0-3}2)`:         {Match: {`12`, `1aaa2`}, UnMatch: {`1aaaa2`}},
}

func main() {
	for pattern, sli := range match {
		re, err := regexp.Compile(pattern)
		if err != nil {
			panic(err)
		}
		for _, str := range sli[Match] {
			if !re.Match(str) {
				panic(fmt.Sprintf("%s not match %s", pattern, str))
			}
		}
		for _, str := range sli[UnMatch] {
			if re.Match(str) {
				panic(fmt.Sprintf("%s match %s", pattern, str))
			}
		}
	}

	invalid := []string{`\u12`, `{123)`}
	for _, p := range invalid {
		_, err := regexp.Compile(p)
		fmt.Println(p, err)
		if err == nil {
			panic(fmt.Sprintf("compile %s should not pass", p))
		}
	}
	fmt.Println("Passed!")
}

Documentation

Overview

Example
package main

import (
	"fmt"
	"regexp"
)

const (
	Yes = 0
	No  = 1
)

var match = map[string][2][]string{
	`(1(\\|\(|c)*2)`:     {Yes: {`12`, `1c((\\2`}, No: {`1*2`}}, // \
	`(1(a|b|c|d)+2)`:     {Yes: {`1a2`, `1aabbdcdc2`}, No: {`12`}},
	`(1a+2)`:             {Yes: {`1a2`, `1aaaaa2`}, No: {`12`}}, // +
	`(1(a|b|c|d)?2)`:     {Yes: {`12`, `1d2`}, No: {`1bc2`}},    // |
	`(1a?2)`:             {Yes: {`12`, `1a2`}, No: {`1aa2`}},
	`(1(a|b|c|d){3}2)`:   {Yes: {`1abc2`}, No: {`1ab2`, `1abcd2`}}, // {n}
	`(1a{3}2)`:           {Yes: {`1aaa2`}, No: {`1aa2`, `1aaaa2`}},
	`(1(a|b|c|豪){0-3}2)`: {Yes: {`12`, `1ab豪2`}, No: {`1abc豪2`}}, // {n-m}
	`(1a{0-3}2)`:         {Yes: {`12`, `1aaa2`}, No: {`1aaaa2`}},
	`.\..`:               {Yes: {`9.9`, `...`}, No: {`.豪.`, `.9`, `9.`, `..`}}, // .
	`.*`:                 {Yes: {``, `.`, `*`, `.*`, `any string`, `$%*^*^*&*)_`}, No: {}},
}

func main() {
	for pattern, sli := range match {
		re, err := Compile(pattern)
		if err != nil {
			panic(err)
		}
		for _, str := range sli[Yes] {
			if !re.Match(str) {
				panic(fmt.Sprintf("%s not match %s", pattern, str))
			}
		}
		for _, str := range sli[No] {
			if re.Match(str) {
				panic(fmt.Sprintf("%s match %s", pattern, str))
			}
		}
	}

	invalid := []string{`\u12`, `{123)`}
	for _, p := range invalid {
		_, err := regexp.Compile(p)
		fmt.Println(p, err)
		if err == nil {
			panic(fmt.Sprintf("compile %s should not pass", p))
		}
	}
	fmt.Println("Passed!")

}
Output:

\u12 error parsing regexp: invalid escape sequence: `\u`
{123) error parsing regexp: unexpected ): `{123)`
Passed!

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Match

func Match(pattern, str string) (bool, error)

Types

type Regexp

type Regexp struct {
	// contains filtered or unexported fields
}

func Compile

func Compile(pattern string) (*Regexp, error)

func (*Regexp) Match

func (re *Regexp) Match(str string) bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL