expression

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

README

expression

Go Version License Build Status PRs Welcome

A lightweight and out-of-the-box Go regular expression wrapper. This package provides convenient helper functions for validating common formats (such as email, IP addresses, domains, and phone numbers) and performing string match/replace operations.

Installation

go get -u github.com/channellabs/expression

Features

  • Validate IPv4 and Internal IPv4 addresses
  • Validate Chinese cellphone numbers (with or without +86 prefix)
  • Validate Chinese landline telephone numbers
  • Validate email addresses
  • Validate web domains (URLs and IPs)
  • Validate time strings (HH:MM format)
  • Simple string matching and regular expression replacement

Quick Start

Validators

The package provides a set of ready-to-use validators. They return true if the input matches the specific pattern, and false otherwise.

package main

import (
	"fmt"
	
	"github.com/channellabs/expression"
)

func main() {
	// Email Validation
	fmt.Println(expression.IsEmail("hello@tom.com"))         // true
	fmt.Println(expression.IsEmail("hello.@foxmail.com"))    // false

	// IPv4 Validation
	fmt.Println(expression.IsIPv4("192.168.0.1"))            // true
	fmt.Println(expression.IsInternalIPv4("127.0.0.1"))      // true
	fmt.Println(expression.IsInternalIPv4("172.10.0.1"))     // false

	// Phone Number Validation (China)
	fmt.Println(expression.IsCellphoneOfChina("+86 13717995000")) // true
	fmt.Println(expression.IsTelephoneOfChina("010-92832839"))    // true

	// Web Domain & URL Validation
	fmt.Println(expression.IsWebDomain("https://www.baidu.com"))  // true
	fmt.Println(expression.IsWebDomain("http:192.168.0.1"))       // false

	// Time Validation (HH:MM)
	fmt.Println(expression.IsTimeHourPoint("23:59"))              // true
	fmt.Println(expression.IsTimeHourPoint("24:01"))              // false
}
Regular Expression Utilities

If you need to match or replace arbitrary regular expressions, the package also provides general utility functions.

package main

import (
	"fmt"
	
	"github.com/channellabs/expression"
)

func main() {
	// Match string
	matched, err := expression.RegularMatchString(`no`, `/usr/bin/which: no hello in ...`)
	fmt.Println(matched, err) // true, <nil>

	// Replace string
	replaced, err := expression.RegularReplaceMatch(
		`\{\{\.NAME\}\}`, 
		"The {{.NAME}} Application starting...", 
		"CallMe",
	)
	fmt.Println(replaced, err) // The CallMe Application starting..., <nil>
}

API Overview

Function Description
IsIPv4(str string) bool Checks if the string is a valid IPv4 address.
IsInternalIPv4(str string) bool Checks if the string is an internal (private) IPv4 address (e.g., 127.x, 10.x, 192.168.x).
IsCellphoneOfChina(str string) bool Checks if the string is a valid Chinese mobile phone number (supports +86 prefix).
IsTelephoneOfChina(str string) bool Checks if the string is a valid Chinese landline telephone number.
IsEmail(str string) bool Checks if the string is a valid email address.
IsWebDomain(str string) bool Checks if the string is a valid web domain or IP-based URL.
IsTimeHourPoint(str string) bool Checks if the string represents a valid time of day (HH:MM).
RegularMatchString(pattern, s string) (bool, error) Reports whether the string s contains any match of the regular expression pattern.
RegularReplaceMatch(pattern, original, replace string) (string, error) Replaces all matches of pattern in original with replace.

Documentation

For more detailed information, please refer to the GoDoc.

License

Licensed under the Apache License, Version 2.0. See LICENSE for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	RegularOfWebDomain    = regexp.MustCompile(`^(http://|https://)?(\d{1,3}(.\d{1,3}){3}|(www.)?[^www.][a-z]+\.[a-z]+)(:\d+)?$`)
	RegularOfIPv4         = regexp.MustCompile(`^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$`)
	RegularOfInternalIPv4 = regexp.MustCompile(`(^127\.)|(^10\.)|(^172\.(1[6-9]|2[0-9]|3[0-1])\.)|(^192\.168\.)`)
	RegularOfCellphone    = regexp.MustCompile(`^(\+86(-|\s)?)?1[3-9][0-9]{9}$`)
	RegularOfTelephone    = regexp.MustCompile(`^(\d{3,4}-)?[1-9][0-9]{4,8}$`)
	RegularOfEmail        = regexp.MustCompile(`^[a-zA-Z0-9]{1,20}(([._\-])?[a-zA-Z0-9]{1,20})*@[a-z0-9]+(\.([a-z]+))+$`)
)

Functions

func IsCellphoneOfChina

func IsCellphoneOfChina(str string) bool

IsCellphoneOfChina regular expression is used to check whether the telephone number is china.

func IsEmail

func IsEmail(str string) bool

IsEmail Use a regular expression to check whether the email address is valid.

func IsIPv4

func IsIPv4(str string) bool

IsIPv4 regular expression is used to check whether the IP address is IPV4.

func IsInternalIPv4

func IsInternalIPv4(str string) bool

IsInternalIPv4 Use a regular expression to check whether the ip address is ipv4.

func IsTelephoneOfChina

func IsTelephoneOfChina(str string) bool

IsTelephoneOfChina regular expression is used to check whether the telephone number is china.

func IsTimeHourPoint

func IsTimeHourPoint(str string) bool

IsTimeHourPoint matched time str is time hour point. str similar 00:01/05:28/19:25/23:59

func IsWebDomain

func IsWebDomain(str string) bool

IsWebDomain Use a regular expression to check whether the web domain is valid.

func RegularMatchString

func RegularMatchString(patten string, s string) (bool, error)

RegularMatchString reports whether the string s contains any match of the regular expression pattern. More complicated queries need to use Compile and the full Regexp interface.

func RegularReplaceMatch

func RegularReplaceMatch(patten string, original string, replace string) (string, error)

RegularReplaceMatch Use a regular expression to replace original string to novel string. The underlying call is regexp.Compile(patten).ReplaceAllString() method.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL