Golang | Splitting a Slice Separated by the Specified Expression

A regular expression is a sequence of characters which define a search pattern. Go language support regular expressions. A regular expression is used for parsing, filtering, validating, and extracting meaningful information from large text, like logs, the output generated from other programs, etc.
In Go regexp, you are allowed to split a slice into substrings separated by the specified expression with the help of Split() method. This method returns a slice of the substrings between those expression matches and the count represents the number of substrings to return. This method is defined under the regexp package, so for accessing this method you need to import the regexp package in your program.

Syntax:

func (re *Regexp) Split(str string, m int) []string

Here, if m>0, then it returns at most m substrings and the last string substring will not split. If m == 0, then it will return nil. If m<0, then it will return all substrings.



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to split a
// slice separated by the specified expression
package main
  
import (
    "fmt"
    "regexp"
)
  
// Main function
func main() {
  
    // Splitting the given slice which is
    // separated by the given expression
    // Using Split() method
    m1 := regexp.MustCompile(`e`)
  
    fmt.Println(m1.Split("GeeksforGeeks", -1))
    fmt.Println(m1.Split("GeeksforGeeks", 1))
    fmt.Println(m1.Split("GeeksforGeeks", 0))
    fmt.Println(m1.Split("GeeksforGeeks", 3))
    fmt.Println(m1.Split("GeeksforGeeks", 2))
    fmt.Println()
  
    m2 := regexp.MustCompile(`123`)
    fmt.Println(m2.Split("123Gee123ks123Geek123s", -2))
    fmt.Println(m2.Split("123Gee123ks123Geek123s", 3))
    fmt.Println(m2.Split("123Gee123ks123Geek123s", 0))
    fmt.Println(m2.Split("123Gee123ks123Geek123s", -1))
    fmt.Println(m2.Split("123Gee123ks123Geek123s", 1))
  
}

chevron_right


Output:

[G  ksforG  ks]
[GeeksforGeeks]
[]
[G  ksforGeeks]
[G eksforGeeks]

[ Gee ks Geek s]
[ Gee ks123Geek123s]
[]
[ Gee ks Geek s]
[123Gee123ks123Geek123s]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to split a
// slice separated by the specified expression
package main
  
import (
    "fmt"
    "regexp"
)
  
// Main function
func main() {
  
    // Creating and initializing a string
    // Using shorthand declaration
    s := "A vv regular vv  expression v is vv a sequence v"+
      " of vv characters v which define a vv search pattern."
  
    // Splitting the given slice which is
    // separated by the given expression
    // Using Split() method
    m := regexp.MustCompile(`v`)
    res1 := m.Split(s, -1)
    fmt.Println(res1)
  
    res2 := m.Split(s, 1)
    fmt.Println(res2)
  
    res3 := m.Split(s, -0)
    fmt.Println(res3)
  
    res4 := m.Split(s, -4)
    fmt.Println(res4)
  
}

chevron_right


Output:

[A    regular     expression   is    a sequence   of    characters   which define a    search pattern.]
[A vv regular vv  expression v is vv a sequence v of vv characters v which define a vv search pattern.]
[]
[A    regular     expression   is    a sequence   of    characters   which define a    search pattern.]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.