How to Do a Case Insensitive Regular Expression in Go

Created
Modified

Using a case-insensitive flag

You can set a case-insensitive flag as the first item in the regex.

You can add a (?i) at the beginning of the pattern to make it case insensitive.

See the following example:

package main

import (
  "fmt"
  "regexp"
)

func main() {
  s := "aBcd"

  r := regexp.MustCompile(`bc`)
  fmt.Println(r.Match([]byte(s)))

  r = regexp.MustCompile(`(?i)bc`)
  fmt.Println(r.FindString(s))
}
false
Bc

For more information about flags, go through https://github.com/google/re2/wiki/Syntax.

Related Tags