Go Package regexp: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 43: Line 43:
func (re *Regexp) ReplaceAllString(src, repl string) string
func (re *Regexp) ReplaceAllString(src, repl string) string
</syntaxhighlight>
</syntaxhighlight>
There is a <code>[[Go_Strings#Replace()|strings.Replace(...)]]</code> function, but it does not take regular expressions.


=TODO=
=TODO=
* https://blog.logrocket.com/deep-dive-regular-expressions-golang/
* https://blog.logrocket.com/deep-dive-regular-expressions-golang/
* https://gobyexample.com/regular-expressions
* https://gobyexample.com/regular-expressions

Revision as of 16:20, 18 March 2024

External

Internal

Overview

import (
	"regexp"
)

var someRegexp *regexp.Regexp
someRegexp = regexp.MustCompile("(\\D+)(\\d+),(\\d)")

groups := someRegexp.FindSubmatch([]byte(s))
if groups == nil {
  // no match 
  ...
}

entireString := groups[0]
firstGroup := groups[1]
secondGroup := groups[2]
thirdGroup := groups[3]

Functions

Matching

regexp.MatchString()

Replacement

Needs a Regexp instance.

regexp.ReplaceAllString()

func (re *Regexp) ReplaceAllString(src, repl string) string

There is a strings.Replace(...) function, but it does not take regular expressions.

TODO