Regular Expression in Golang

(Hal) #1

Hi everybody,

I am working on a function that validates the update of a user’s password.

I need to reinforce the password with this regular expression:


But I do not know how to implement it in my code.

This is my code :

// UpdatePassword  uptdate the password for this user.
  func UpdatePassword(unitID string, loginID string, password string) (err error) {
f := GetFederation(unitID)
ldap := connections[f.Connection]
hash, err := HashPassword(password, "SSHA", nil)
if err != nil {
    msg := fmt.Sprintf("password hash generation failed for %s. Error: %s", loginID, err.Error())
    return errors.New(msg)
originalPasswordHash, err := mdb.GetUserPasswordHash(loginID, f.ID)
if err != nil {
    if err.Error() == mgo.ErrNotFound.Error() && ldap != nil {
        var profile map[string]string
        profile, err = ldap.GetProfile(loginID)
        if err != nil {
            debug.Println("get Profile error ", err)
            return err
        login := profile[config.Connections[f.ID].LoginField]
        loginID, originalPasswordHash, err = ldap.GetPasswordHash(login)
        if err != nil {
            return err
    } else {
        return err
if err = mdb.ChangeUserPassword(loginID, f.ID, hash); err != nil {
    debug.Println("ChangeUserPassword error ", err)
    return err

if ldap != nil {
    err = ldap.ChangePassword(loginID, password)
    if err != nil {
        info.Printf("revert password change in DB for user %s", loginID)
        if err = mdb.ChangeUserPassword(loginID, f.ID, originalPasswordHash); err != nil {
            return fmt.Errorf("cannot revert password for user %s in DB", loginID)
        return fmt.Errorf("cannot change password for user %s in ldap", loginID)

return nil


Do you have an idea?

Thank you so much

(Onezino Moreira) #2

Have you try the regex package ?

(Hal) #3

Yes i’m working on it.

But i’m not sure how can implement this in my function,

Have you got an example which i can implement inside my function ? Thanks

PS: I’m Beginner

(catacombs) #4

Check the documentation for the regex package, there should be examples in there. I use that package a lot and also need to refer to the instructions.

(Onezino Moreira) #5

Hi. Your regex don`t compile

	matched, err := regexp.MatchString(`foo.*`, "seafood")
	fmt.Println(matched, err)

A simple example. So if you run this before your change password function and check error.

(Norbert Melzer) #6

The problem is, that the given regex got mangled by the forum, since OP didn’t use backticks to enclose it.