verbalexpressions

package module
Version: v0.0.0-...-4d76a10 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2020 License: MIT Imports: 5 Imported by: 3

README

GoVerbalExpressions

Build Status Coverage Status

Go VerbalExpressions implementation

VerbalExpression is a concept to help building difficult regular expressions.

See online doc here: http://godoc.org/github.com/VerbalExpressions/GoVerbalExpressions

Other Implementations

You can see an up to date list of all ports on VerbalExpressions.github.io.

Installation

Use this command line:

go get github.com/VerbalExpressions/GoVerbalExpressions

This will install package in your $GOPATH and you will be ready to import it.

Examples


// import with a nice name
import (
    "github.com/VerbalExpressions/GoVerbalExpressions" // imports verbalexpressions package
    "fmt"
)

func main () {
    v := verbalexpressions.New().
            StartOfLine().
            Then("http").
            Maybe("s").
            Then( "://").
            Maybe("www.").
            AnythingBut(" ").
            EndOfLine()

    testMe := "https://www.google.com"
    
    if v.Test(testMe) {
       fmt.Println("You have a valid URL") 
    } else {
       fmt.Println("URL is incorrect") 
    }
}

We try to give alias method and/or helpers. For example:


    // s will be "We have a blue house"
    s := verbalexpressions.New().Find("red").Replace("We have a red house", "blue")

    // c will be:
    // [
    //    ["http://www.google.com",  "http://", "www.google.com"]
    // ]
    c := verbalexpressions.New().
        BeginCapture().
            Find("http").Maybe("s").Find("://").
        EndCapture().
        BeginCapture().
            Find("www.").Anything().
        EndCapture().
        Captures("http://www.google.com")

    // check c[0][1] => http://
    //       c[0][2] => www.google.com

Documentation

Overview

This is a Go implementation of VerbalExpressions for other languages. Check http://VerbalExpressions.github.io to know the other implementations.

VerbalExperssions is a way to build complex regular expressions with a verbal language.

The repo name is "GoVerbalExpressions" but the real package name is "verbalexpressions". So, to import verbalexpressions package, just do:

import "github.com/VerbalExpressions/GoVerbalExpressions"

Then, use "verbalexpressions" as prefix. There is a simple example

Use "New()" factory then you can chain calls. Go syntax allows you to set new line after seperators:

v := verbalexpressions.New().
	StartOfLine().
	Find("foo").
	Word().
	Anything().
	EndOfLine()

Then, you can use "Test()" method to check if your string matches expression.

You may get the regexp.Regexp structure using "Regex()" method, then use common methods to split, replace, find submatches and so on... as usual

There are some helpers that use direct call to the regexp package:

- Replace - Captures - Test

Copyright 2013 Patrice FERLET Use of this source code is governed by MIT-style license that can be found in the LICENSE file

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Flag

type Flag uint
const (
	MULTILINE Flag = 1 << iota
	IGNORE_CASE
	DOTALL
	UNGREEDY
	GLOBAL
)

type VerbalExpression

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

VerbalExpression structure to create expression

func New

func New() *VerbalExpression

Instanciate a new VerbalExpression. You should use this method to initalize some internal var.

Example:

v := verbalexpression.New().Find("foo")

func (*VerbalExpression) And

Add another VerbalExpression to the current. Usefull to concatenate several complex search patterns

func (*VerbalExpression) Any

Any accepts caracters to be matched

Example
s := "foo1 foo5 foobar"
v := verbalexpressions.New().Find("foo").Any("1234567890").Regex().FindAllString(s, -1)
fmt.Println(v)
Output:

[foo1 foo5]

func (*VerbalExpression) AnyOf

AnyOf is an alias to Any

func (*VerbalExpression) Anything

func (v *VerbalExpression) Anything() *VerbalExpression

Anything will match any char

func (*VerbalExpression) AnythingBut

func (v *VerbalExpression) AnythingBut(s string) *VerbalExpression

AnythingBut will match anything excpeting the given string.

Example
s := "This is a simple test"
v := verbalexpressions.New().AnythingBut("ie").Regex().FindAllString(s, -1)
fmt.Println(v)
Output:

[Th s  s a s mpl  t st]

func (*VerbalExpression) BeginCapture

func (v *VerbalExpression) BeginCapture() *VerbalExpression

Start to capture something, stop with EndCapture()

func (*VerbalExpression) Br

Alias to LineBreak

func (*VerbalExpression) Captures

func (v *VerbalExpression) Captures(s string) [][]string

Returns a slice of results from captures. If you didn't apply BeginCapture() and EndCapture(), the slices will return slice of []string where []string is length 1, and 0 index is the global capture

Example
s := `This should get barsystem and whatever...
And there, another barelement to fetch`
// get "bar" followed by a word
v := verbalexpressions.New().Anything().
	BeginCapture().
	Find("bar").Word().
	EndCapture()

res := v.Captures(s)

// walk results
for _, element := range res {
	// i reprensent capture count and element is a
	// slice with captures (0 = global find, 1 = first capture)
	fmt.Printf("Global capture: %s\n", element[0])
	fmt.Printf("First capture: %s\n", element[1])
}
Output:

Global capture: This should get barsystem
First capture: barsystem
Global capture: And there, another barelement
First capture: barelement

func (*VerbalExpression) EndCapture

func (v *VerbalExpression) EndCapture() *VerbalExpression

Stop capturing expresions parts

func (*VerbalExpression) EndOfLine

func (v *VerbalExpression) EndOfLine() *VerbalExpression

EndOfLine tells verbalexpressions to match a end of line. Warning, to check multiple line, you must use SearchOneLine(true)

func (*VerbalExpression) Find

Find seeks string. The string MUST be there (unlike Maybe() method)

Example
s := "foo bar baz"
v := verbalexpressions.New().Find("bar")
fmt.Println(v.Test(s))
Output:

true

func (*VerbalExpression) LineBreak

func (v *VerbalExpression) LineBreak() *VerbalExpression

LineBreak to find "\n" or "\r\n"

func (*VerbalExpression) MatchAllWithDot

func (v *VerbalExpression) MatchAllWithDot(enable bool) *VerbalExpression

MatchAllWithDot lets VerbalExpression matching "." for everything including \n, \r, and so on

func (*VerbalExpression) Maybe

Maybe will search string zero on more times

func (*VerbalExpression) Multiple

func (v *VerbalExpression) Multiple(s string, mults ...int) *VerbalExpression

Multiply string s expression

Multiple(value string [, min int[, max int]])

This method accepts 1 to 3 arguments, argument 2 is min, argument 3 is max:

// get "foo" at least one time
v.Multiple("foo")
v.Multiple("foo", 1)

// get "foo" 0 or more times
v.Multiple("foo", 0)

//get "foo" 0 or 1 times
v.Multiple("foo", 0, 1)

// get "foo" 0 to 10 times
v.Multiple("foo",0 ,10)

//get "foo" at least 10 times
v.Multiple("foo", 10)

//get "foo" exactly 10 times
v.Multiple("foo", 10, 10)

//get "foo" from 1 to 10 times
v.Multiple("foo", 1, 10)

func (*VerbalExpression) Not

func (v *VerbalExpression) Not(value string) *VerbalExpression

Not invert Find, meaning search something excepting "value". This is different than SomethingBut or AnythingBut that works with a list of caracters. Note that this method is not exactly the same as PCRE system.

While PCRE allows: foo(?!bar)baz, "foobarrbaz" matches (note the double r). With our method, this doesn't match. But: "foobarbaz" doen't match (so it's ok). And "fooXXXbaz" matches also.

func (*VerbalExpression) Or

Or, chains an alternative VerbalExpression

Example
s := "This is a foo and a bar there"
v1 := verbalexpressions.New().Find("bar")
v := verbalexpressions.New().
	Find("foo").
	Or(v1)

fmt.Println(v.Regex().FindAllString(s, -1))
Output:

[foo bar]

func (*VerbalExpression) Range

func (v *VerbalExpression) Range(args ...interface{}) *VerbalExpression

Range accepts an even number of arguments. Each pair of values defines start and end of range. Think like this: Range(from, to [, from, to ...])

Example
s := "This 1 is 55 a TEST"
v := verbalexpressions.New().Range("a", "z", 0, 9)
res := v.Regex().FindAllString(s, -1)
fmt.Println(res)
Output:

[h i s 1 i s 5 5 a]

func (*VerbalExpression) Regex

func (v *VerbalExpression) Regex() *regexp.Regexp

Regex returns the regular expression to use to test on string.

func (*VerbalExpression) Replace

func (v *VerbalExpression) Replace(src string, dst string) string

Replace alias to regexp.ReplaceAllString. It replace the found expression from string src by string dst

Example
s := "We've got a red house"
res := verbalexpressions.New().Find("red").Replace(s, "blue")
fmt.Println(res)
Output:

We've got a blue house

func (*VerbalExpression) SearchOneLine

func (v *VerbalExpression) SearchOneLine(oneline bool) *VerbalExpression

SearchOneLine deactivates "multiline" mode if online argument is true Default is false

func (*VerbalExpression) Something

func (v *VerbalExpression) Something() *VerbalExpression

Something matches at least one char

func (*VerbalExpression) SomethingBut

func (v *VerbalExpression) SomethingBut(s string) *VerbalExpression

Same as Something but excepting chars given in string "s"

func (*VerbalExpression) StartOfLine

func (v *VerbalExpression) StartOfLine() *VerbalExpression

StartOfLine seeks the begining of a line. As EndOfLine you should use SearchOneLine(true) to test multiple lines

func (*VerbalExpression) StopAtFirst

func (v *VerbalExpression) StopAtFirst(enable bool) *VerbalExpression

func (*VerbalExpression) Tab

Tab fetch tabulation char (\t)

func (*VerbalExpression) Test

func (v *VerbalExpression) Test(s string) bool

Test return true if verbalexpressions matches something in string "s"

func (*VerbalExpression) Then

Alias to Find()

func (*VerbalExpression) WithAnyCase

func (v *VerbalExpression) WithAnyCase(sensitive bool) *VerbalExpression

WithAnyCase asks verbalexpressions to match with or without case sensitivity

func (*VerbalExpression) Word

Word matches any word (containing alpha char)

Jump to

Keyboard shortcuts

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