golang tutorial - Go Regular Expressions | Regular Expressions In Golang - golang - go programming language - google go - go language - go program - google language



Golang Regular Expressions

  • A regular expression, regex or regexp (sometimes called a rational expression) is nothing but a sequence of characters that define a search pattern.
  • Usually this pattern is then used by string searching algorithms for "find" or "find and replace" operations on strings.
  • Typical example is, Phone number - It should be numbers and it can be characters. Writing a regex pattern to check whether the given is all numbers or not becomes too famous.
 regular expressions

Regex Usage:

  • A regular expression, regex or regexp (sometimes called a rational expression) is nothing but a sequence of characters that define a search pattern.
  • Usually this pattern is then used by string searching algorithms for "find" or "find and replace" operations on strings.
  • Typical example is, Phone number - It should be numbers and it can be characters. Writing a regex pattern to check whether the given is all numbers or not becomes too famous.

Regex Quantification :

  • A quantifier after a token (such as a character) or group specifies how often that preceding element is allowed to occur.
  • ? The question mark indicates zero or one occurrences of the preceding element.
  • For example, colou?r matches both "color" and "colour".
  • * The asterisk indicates zero or more occurrences of the preceding element.
  • For example, ab*c matches "ac", "abc", "abbc", "abbbc", and so on.
  • + The plus sign indicates one or more occurrences of the preceding element.
  • For example, ab+c matches "abc", "abbc", "abbbc", and so on, but not "ac".
  • {n}[19] The preceding item is matched exactly n times.
  • {min,}[19] The preceding item is matched min or more times.
  • {min,max}[19] The preceding item is matched at least min times, but not more than max times.

Regex Examples :

  • a|b* denotes {ε, "a", "b", "bb", "bbb", …}
  • (a|b)* denotes the set of all strings with no symbols other than "a" and "b", including the empty string: {ε, "a", "b", "aa", "ab", "ba", "bb", "aaa", …}
  • ab*(c|ε) denotes the set of strings starting with "a", then zero or more "b"s and finally optionally a "c": {"a", "ac", "ab", "abc", "abb", "abbc", …}
  • (0|(1(01*0)*1))* denotes the set of binary numbers that are multiples of 3: { ε, "0", "00", "11", "000", "011", "110", "0000", "0011", "0110", "1001", "1100", "1111", "00000", … }
  • .at matches any three-character string ending with "at", including "hat", "cat", and "bat".
  • [hc]at matches "hat" and "cat".
  • [^b]at matches all strings matched by .at except "bat".
  • [^hc]at matches all strings matched by .at other than "hat" and "cat".
  • ^[hc]at matches "hat" and "cat", but only at the beginning of the string or line.
  • [hc]at$ matches "hat" and "cat", but only at the end of the string or line.
  • \[.\] matches any single character surrounded by "[" and "]" since the brackets are escaped, for example: "[a]" and "[b]".
  • s.* matches s followed by zero or more characters, for example: "s" and "saw" and "seed".
  • [hc]?at matches "hat", "cat", and "at".
  • [hc]*at matches "hat", "cat", "hhat", "chat", "hcat", "cchchat", "at", and so on.
  • [hc]+at matches "hat", "cat", "hhat", "chat", "hcat", "cchchat", and so on, but not "at".
  • cat|dog matches "cat" or "dog".

Regex in go programming

 regular expressions
package main

	import "bytes"
import "fmt"
import "regexp"

	func main() {
// This tests whether a pattern matches a string.
	    match, _ := regexp.MatchString("p([a-z]+)ch", "peach")
    fmt.Println(match)

// Above we used a string pattern directly, but for other regexp tasks you’ll need to Compile an optimized Regexpstruct.
	    r, _ := regexp.Compile("p([a-z]+)ch")
// Many methods are available on these structs. Here’s a match test like we saw earlier.
	    fmt.Println(r.MatchString("peach"))
// This finds the match for the regexp.
	    fmt.Println(r.FindString("peach punch"))
// This also finds the first match but returns the start and end indexes for the match instead of the matching text
.	    fmt.Println(r.FindStringIndex("peach punch"))
// The Submatch variants include information about both the whole-pattern matches and the submatches within those matches. For example this will return information for both p([a-z]+)ch and ([a-z]+).
	    fmt.Println(r.FindStringSubmatch("peach punch"))
// Similarly this will return information about the indexes of matches and submatches.
	    fmt.Println(r.FindStringSubmatchIndex("peach punch"))
// The All variants of these functions apply to all matches in the input, not just the first. For example to find all matches for a regexp.
	    fmt.Println(r.FindAllString("peach punch pinch", -1))
// These All variants are available for the other functions we saw above as well.
	    fmt.Println(r.FindAllStringSubmatchIndex(
        "peach punch pinch", -1))
// Providing a non-negative integer as the second argument to these functions will limit the number of matches.
	    fmt.Println(r.FindAllString("peach punch pinch", 2))
// Our examples above had string arguments and used names like MatchString. We can also provide []bytearguments and drop String from the function name.
	    fmt.Println(r.Match([]byte("peach")))
// When creating constants with regular expressions you can use the MustCompile variation of Compile. A plain Compilewon’t work for constants because it has 2 return values.
	    r = regexp.MustCompile("p([a-z]+)ch")
    fmt.Println(r)
// The regexp package can also be used to replace subsets of strings with other values.
	    fmt.Println(r.ReplaceAllString("a peach", "<fruit>"))
// The Func variant allows you to transform matched text with a given function.
	    in := []byte("a peach")
    out := r.ReplaceAllFunc(in, bytes.ToUpper)
    fmt.Println(string(out))
}

	$ go run regular-expressions.go 
click below button to copy the code. By - golang tutorial - team
golang , gopro , google go , golang tutorial , google language , go language , go programming language

output of the above program

true
true
peach
[0 5]
[peach ea]
[0 5 1 3]
[peach punch pinch]
[[0 5 1 3] [6 11 7 9] [12 17 13 15]]
[peach punch]
true
p([a-z]+)ch
a <fruit>
a PEACH

Related Searches to Regular Expressions In Golang