DEV Community 👩‍💻👨‍💻

mohaned mashaly
mohaned mashaly

Posted on

Regex in Go

One day i was developing a project in Django and I was using the User model which is used for authenticating the user's to check if they have account or not, it's really an interesting model and the User model can create an object User it's an instance from class User which have attributes like username, password, firstname ,etc... and method like is_authenticated which is the most famous one for me to check if the user is authenticated or not(have an account)I think i will talk about Django User Model in elaboration in another post.

so to get to our topic which is regular expressions(regex) in computer science and in GoLang in specific so i wondered how the Django User model when used in filling forms like the ones in the Signup Page when user is trying to create a username and Email, password and the Application or the Website is restricting that the Email must contain the '@' and the username must be 6 character at-least and the Email must contain a '@' and '.com' or '.org' or any extension which is valid i think it comes to your mind using nested if's to check the length of the passed values and check that the password map to a certain schema this will result in a not so clean and efficient code using Nested if's.

so the regular expression solved this problem i read about regular expression in python but but i didn't understand it very will till i had a tutorial in it as part of theory of computation course this tutorial topic was about regular expression in computation theory regular expression(regex) is a symbols or characters used to define a schema for a certain function for example if i am trying to check this number using regex .

i will used regular expressions and regex interchangeably so we will use 0 for example to generate even numbers always which will have the following schema (00)0 whatever number given it will always generate an even number if the right most zero is 1 the number will be 2 if it's 0 it will be zero this also follows the schema for all even numbers which 2K so this a little about regex from the theory perspective now to the practical one.

GoLang has a library called regexp which is used to check if the values given by user is valid or not for example if i want the username to contain characters only and have a length of at-least 3 characters i will make the following regex [a-zA-Z]{3,} so let's explain it.

the a-zA-z it means the the username can contain uppercase or lower case in it from a to z or from A to Z in uppercase and the {} to specify the number of characters in the string so the character must have at-least 3 characters and at-most no limit so if the username expression was like this [a-zA-Z]{3,9} it means at-least 3 characters and at-most 9 characters.

i will provide a very small subset of common regular expression keywords this is not by any means a comprehensive list if you're interested to know more you will find link in the fun to read section

Keywords :-

  • : one or more appearance (it means there has to be at-least one character or number or whatever the hell) (0-9)+

0-9 : any number from 0 to 9

  • : zero or more appearance (it means it can be empty (epsilon)) example (0-9)*

$ : the expression should end with whatever before this sign for example if the expression ends with 0-9$ then it have to end with a number

a-z : any lower case character

A-Z : any upper case character

@ : it literally means the @ sign in the email to verify if this is an email or not
the following expression is used to verify the user inputs like username, email, password (Warning password regex is not very safe you should add a little expressions to it)

the code in Go :-

func Signupvalidation(username string, FirstName string, LastName string, Email string, password string)string {
usernamevalidation, _ := regexp.MatchString("[a-zA-Z0-9]{3,}",username)
FirstNamevalidation, _ := regexp.MatchString("[a-zA-Z0-9]{3,}",FirstName)
LastNamevalidation, _ := regexp.MatchString("[a-zA-Z0-9]{3,}",LastName)
EmailValidation, _ := regexp.MatchString("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+[a-zA-Z0-9-.]+$",Email)
PasswordValidation, _ := regexp.MatchString("[a-zA-Z]{10,}",password)

if usernamevalidation != true {
    return "userName is not valid"

if FirstNamevalidation != true {
    return "First Name is not valid"

if LastNamevalidation != true{
    return "Last Name is not valid"

if EmailValidation != true {
    return "Email is not valid"

if PasswordValidation != true{
    return "Password is not valid"

return "valid"


Hope you find this Article fruitful

full version of the code here :-

fun to read :-

Top comments (0)

You guessed it

The most popular item in our shop is the stickers. Grab a pack today (with free shipping)!