10 examples of 'golang regexp' in Go

Every line of 'golang regexp' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
1120func (c *OpContext) regexp(v Value) *regexp.Regexp {
1121 v = Unwrap(v)
1122 if isError(v) {
1123 return matchNone
1124 }
1125 switch x := v.(type) {
1126 case *String:
1127 if x.RE != nil {
1128 return x.RE
1129 }
1130 // TODO: synchronization
1131 p, err := regexp.Compile(x.Str)
1132 if err != nil {
1133 // FatalError? How to cache error
1134 c.AddErrf("invalid regexp: %s", err)
1135 x.RE = matchNone
1136 } else {
1137 x.RE = p
1138 }
1139 return x.RE
1140
1141 case *Bytes:
1142 if x.RE != nil {
1143 return x.RE
1144 }
1145 // TODO: synchronization
1146 p, err := regexp.Compile(string(x.B))
1147 if err != nil {
1148 c.AddErrf("invalid regexp: %s", err)
1149 x.RE = matchNone
1150 } else {
1151 x.RE = p
1152 }
1153 return x.RE
1154
1155 default:
1156 c.typeError(v, StringKind|BytesKind)
1157 return matchNone
1158 }
1159}
191func compileRegexp(regStr string) *regexp.Regexp {
192 if regStr == "" {
193 return nil
194 }
195
196 return regexp.MustCompile("^" + regStr + "$")
197}
214func (m *GetRequest) GetRegexp() bool {
215 if m != nil {
216 return m.Regexp
217 }
218 return false
219}
206func (sc *StringConstraint) RegexpString(pat string) *StringConstraint {
207 return sc.Regexp(regexp.MustCompile(pat))
208}
246func (m *Query) GetRegexp() *RegexpQuery {
247 if x, ok := m.GetQuery().(*Query_Regexp); ok {
248 return x.Regexp
249 }
250 return nil
251}
1100func compileRegexp(re, flags string) (*regexp.Regexp, error) {
1101 re = strings.ReplaceAll(re, "(?<", "(?P<")
1102 if strings.ContainsRune(flags, 'i') {
1103 re = "(?i)" + re
1104 }
1105 if strings.ContainsRune(flags, 'm') {
1106 re = "(?s)" + re
1107 }
1108 r, err := regexp.Compile(re)
1109 if err != nil {
1110 return nil, fmt.Errorf("invalid regular expression %q: %v", re, err)
1111 }
1112 return r, nil
1113}
93func ImportRegex(env *gdsl.Glisp) {
94 env.AddFunction("regexp-compile", RegexpCompile)
95 env.AddFunction("regexp-find-index", RegexpFind)
96 env.AddFunction("regexp-find", RegexpFind)
97 env.AddFunction("regexp-match", RegexpFind)
98}
31func main() {
32 // "qemu+tcp://root@wally131.cit.tu-berlin.de/system"
33 // con, err := libvirt.NewVirConnection(os.Args[1])
34 // check(err)
35 // fmt.Println(con.GetHostname())
36 // fmt.Println(con.GetLibVersion())
37 // fmt.Println(con.GetType())
38 // fmt.Println(con.GetCapabilities())
39 // fmt.Println(con.GetNodeInfo())
40
41 r := regexp.MustCompile("^" + "aaa" + "(-[0-9]+)?" + ".bbb" + "$")
42 log.Println(r.MatchString("aaa.bbb"))
43 log.Println(r.MatchString("aaa-.bbb"))
44 log.Println(r.MatchString("aaa--.bbb"))
45 log.Println(r.MatchString("aaa .bbb"))
46 log.Println(r.MatchString("aaa-0.bbb"))
47 log.Println(r.MatchString("aaa-0a.bbb"))
48 log.Println(r.MatchString("aaa-11.bbb"))
49}
619func getRegexpForVar(v interface{}) string {
620 s := v.(string)
621
622 if _, err := strconv.Atoi(s); err == nil {
623 return "(\\d+)"
624 }
625
626 if _, err := strconv.ParseFloat(s, 32); err == nil {
627 return "([+-]?([0-9]*[.])?[0-9]+)"
628 }
629
630 return "(.*)"
631}
63func init() {
64 core.TypeRegistry.Register(RegExp{})
65}

Related snippets