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 ¶
- type Flag
- type VerbalExpression
- func (v *VerbalExpression) And(ve *VerbalExpression) *VerbalExpression
- func (v *VerbalExpression) Any(s string) *VerbalExpression
- func (v *VerbalExpression) AnyOf(s string) *VerbalExpression
- func (v *VerbalExpression) Anything() *VerbalExpression
- func (v *VerbalExpression) AnythingBut(s string) *VerbalExpression
- func (v *VerbalExpression) BeginCapture() *VerbalExpression
- func (v *VerbalExpression) Br() *VerbalExpression
- func (v *VerbalExpression) Captures(s string) [][]string
- func (v *VerbalExpression) EndCapture() *VerbalExpression
- func (v *VerbalExpression) EndOfLine() *VerbalExpression
- func (v *VerbalExpression) Find(s string) *VerbalExpression
- func (v *VerbalExpression) LineBreak() *VerbalExpression
- func (v *VerbalExpression) MatchAllWithDot(enable bool) *VerbalExpression
- func (v *VerbalExpression) Maybe(s string) *VerbalExpression
- func (v *VerbalExpression) Multiple(s string, mults ...int) *VerbalExpression
- func (v *VerbalExpression) Not(value string) *VerbalExpression
- func (v *VerbalExpression) Or(ve *VerbalExpression) *VerbalExpression
- func (v *VerbalExpression) Range(args ...interface{}) *VerbalExpression
- func (v *VerbalExpression) Regex() *regexp.Regexp
- func (v *VerbalExpression) Replace(src string, dst string) string
- func (v *VerbalExpression) SearchOneLine(oneline bool) *VerbalExpression
- func (v *VerbalExpression) Something() *VerbalExpression
- func (v *VerbalExpression) SomethingBut(s string) *VerbalExpression
- func (v *VerbalExpression) StartOfLine() *VerbalExpression
- func (v *VerbalExpression) StopAtFirst(enable bool) *VerbalExpression
- func (v *VerbalExpression) Tab() *VerbalExpression
- func (v *VerbalExpression) Test(s string) bool
- func (v *VerbalExpression) Then(s string) *VerbalExpression
- func (v *VerbalExpression) WithAnyCase(sensitive bool) *VerbalExpression
- func (v *VerbalExpression) Word() *VerbalExpression
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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 ¶
func (v *VerbalExpression) And(ve *VerbalExpression) *VerbalExpression
Add another VerbalExpression to the current. Usefull to concatenate several complex search patterns
func (*VerbalExpression) Any ¶
func (v *VerbalExpression) Any(s string) *VerbalExpression
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 ¶
func (v *VerbalExpression) AnyOf(s string) *VerbalExpression
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) 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 ¶
func (v *VerbalExpression) Find(s string) *VerbalExpression
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 ¶
func (v *VerbalExpression) Maybe(s string) *VerbalExpression
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 ¶
func (v *VerbalExpression) Or(ve *VerbalExpression) *VerbalExpression
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 ¶
func (v *VerbalExpression) Tab() *VerbalExpression
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 ¶
func (v *VerbalExpression) Then(s string) *VerbalExpression
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 ¶
func (v *VerbalExpression) Word() *VerbalExpression
Word matches any word (containing alpha char)