Categories
Regular Expression

Literals in Regular Expression

Literals are the simplest form of pattern matching in regular expressions. They will simply succeed whenever that literal is found.

func Match 

func Match(pattern string, b []byte) (matched bool, err error)

Match reports whether the byte slice b contains any match of the regular expression pattern.

Example Code

package main

import (
	"fmt"
	"regexp"
)

func main() {
        pattern:="Hello"
        // Match() function return a boolean type value 
        // with boolean it limit us from extracting the match string value
	matched, err := regexp.Match(pattern, []byte(`World Hello`))
	// print value return from Match() func
	fmt.Println(matched, err)
	if matched {
	   fmt.Println("Found")
	} else {
	   fmt.Println("Not Found")
	}
}

Go Playground

https://play.golang.org/p/ts3SyaxKQgF

Resources

go doc regexp/syntax

https://pkg.go.dev/regexp?tab=doc#pkg-overview

Categories
regexp Regular Expression

Regular Expression

regrex package

https://golang.org/pkg/regexp/

On-line Regular Expression

https://regex101.com/

Meta Characters

Metacharacters are characters that are interpreted in a special way by a RegEx engine. Here’s a list of metacharacters:

[] . ^ $ * + ? {} () \ |

[] – Square brackets

Square brackets specifies a set of characters you wish to match.

. – Period

A period matches any single character (except newline '\n').

 – Caret

The caret symbol ^ is used to check if a string starts with a certain character.

$ – Dollar

The dollar symbol $ is used to check if a string ends with a certain character.

* – Star

The star symbol * matches zero or more occurrences of the pattern left to it.

+ – Plus

The plus symbol + matches one or more occurrences of the pattern left to it.

? – Question Mark

The question mark symbol ? matches zero or one occurrence of the pattern left to it.

{} – Braces

Consider this code: {n,m}. This means at least n, and at most m repetitions of the pattern left to it.

| – Alternation

Vertical bar | is used for alternation (or operator).

() – Group

Parentheses () is used to group sub-patterns.

\ – Backslash

Backlash \ is used to escape various characters including all metacharacters.

Special Sequences

Special sequences make commonly used patterns easier to write.