github.com/enetx/g@v1.0.80/examples/strings_regexp.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  
     7  	"github.com/enetx/g"
     8  )
     9  
    10  func main() {
    11  	// Define a regular expression pattern
    12  	pattern := regexp.MustCompile(`(?m)(post-)(?:\d+)`)
    13  
    14  	// Replace the first occurrence of the pattern in the string
    15  	g.String("post-55").ReplaceRegexp(pattern, "${1}38").Print()
    16  	// Output: post-38
    17  
    18  	// Find the first match of the pattern in the string
    19  	o := g.String("x post-55 x").FindRegexp(pattern)
    20  	// Output: post-55
    21  
    22  	// switch pattern
    23  	switch {
    24  	case o.IsSome():
    25  		o.Some().Print()
    26  	default:
    27  		fmt.Println("not found")
    28  	}
    29  
    30  	// If no match is found, provide a default value
    31  	g.String("post-not-found").FindRegexp(pattern).UnwrapOr("post-333").Print()
    32  	// Output: post-333
    33  
    34  	// Find all matches of the pattern in the string
    35  	g.String("some post-55 not found post-31 post-22").FindAllRegexp(pattern).Unwrap().Print()
    36  	// Output: Slice[post-55, post-31, post-22]
    37  
    38  	// Find a specific number of matches of the pattern in the string
    39  	g.String("some post-55 not found post-31 post-22").FindAllRegexpN(pattern, 2).Some().Print()
    40  	// Output: Slice[post-55, post-31]
    41  
    42  	// Get the starting indices of the first match of the pattern in the string
    43  	g.String("post-55").IndexRegexp(pattern).Some().Print()
    44  	// Output: Slice[0, 7]
    45  
    46  	// Find the submatches of the first match of the pattern in the string
    47  	g.String("some post-55 not found post-31").FindSubmatchRegexp(pattern).Some().Print()
    48  	// Output: Slice[post-55, post-]
    49  
    50  	// Find all submatches of the pattern in the string
    51  	g.String("some post-55 not found post-31 post-22").FindAllSubmatchRegexp(pattern).Some().Print()
    52  	// Output: Slice[Slice[post-55, post-], Slice[post-31, post-], Slice[post-22, post-]]
    53  
    54  	// Find a specific number of submatches of the pattern in the string
    55  	g.String("some post-55 not found post-31 post-22").FindAllSubmatchRegexpN(pattern, 2).Some().Print()
    56  	// Output: Slice[Slice[post-55, post-], Slice[post-31, post-]]
    57  
    58  	// Split the string using the regular expression pattern
    59  	g.String("some test for split n").SplitRegexp(*regexp.MustCompile(`\s`)).Print()
    60  	// Output: Slice[some, test, for, split, n]
    61  
    62  	// Split the string using the regular expression pattern, limiting the number of splits
    63  	g.String("some test for split n").SplitRegexpN(*regexp.MustCompile(`\s`), 2).Some().Print()
    64  	// Output: Slice[some, test for split n]
    65  
    66  	// Check if the string contains a match of the regular expression
    67  	fmt.Println(g.String("some test").ContainsRegexp(`\s`).Unwrap())
    68  	// Output: true
    69  
    70  	// Check if the string contains matches for all the provided regular expressions
    71  	fmt.Println(g.String("some test 1").ContainsRegexpAll(`\s`, `\d`).Unwrap())
    72  	// Output: true
    73  
    74  	// Check if the string contains a match for any of the provided regular expressions
    75  	fmt.Println(g.String("some test").ContainsRegexpAny(`\s`, `\d`).Unwrap())
    76  	// Output: true
    77  }