Compile parses a regular expression and returns, if successful, a Regexp object that can be used to match against lines of text.
(expr string)
| 28 | // Compile parses a regular expression and returns, if successful, |
| 29 | // a Regexp object that can be used to match against lines of text. |
| 30 | func Compile(expr string) (*Regexp, error) { |
| 31 | re, err := syntax.Parse(expr, syntax.Perl) |
| 32 | if err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | sre := re.Simplify() |
| 36 | prog, err := syntax.Compile(sre) |
| 37 | if err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | if err := toByteProg(prog); err != nil { |
| 41 | return nil, err |
| 42 | } |
| 43 | r := &Regexp{ |
| 44 | Syntax: re, |
| 45 | expr: expr, |
| 46 | } |
| 47 | if err := r.m.init(prog); err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | return r, nil |
| 51 | } |
| 52 | |
| 53 | func (r *Regexp) Match(b []byte, beginText, endText bool) (end int) { |
| 54 | return r.m.match(b, beginText, endText) |