Hi, I want to implement a generic Regex validator for a generic email, like "aaa@aaa.com", but I can't get it to work, this is the one I used to apply always:

"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}\$"

But if I try to use that one directly, I get this error: Parse error: Invalid escape sequence

Therefore, to solve this error I have transformed it into the following:

"^[\w\-\.]+@[\w\-]+\.[\w\-]{2,4}$"

But it's still not working correctly, and I think it may be in the way that I'm trying to get the text and validate it.

This is the code (inside the same function):

var regex = RegEx.new()
regex.compile("^[\w\-\.]+@[\w\-]+\.[\w\-]{2,4}$")
...
elif regex.search(emailRegisterField.text.strip_edges()):
emailRegisterMessage.text = "EMAIL_INCORRECT"
emailRegisterMessage.visible = true

  • Mario999 Why do you escape the first square bracket?

    Shouldn't that regex string be just:

    "^[\\w\\-\\.]+@[\\w\\-]+\\.[\\w\\-]{2,4}$"

Shouldn't those backslashes be backslashed in a string literal?

    xyz

    Yes, but when I copy it here they are automatically deleted

    In code they are backslashed.

    • xyz replied to this.

      Mario999
      Paste your code between lines containing ~~~ so we can see what is actually happening.

      Btw why don't you just paste the regex at https://regex101.com/ and check if it parses in the flavor supported by Godot.

        xyz

        I think an image is more visual, here is the area where the Regex is generated and applied:

        xyz

        Honestly, I didn't know that page exactly, thanks.

        It works btw.

        • xyz replied to this.

          xyz

          Nah, the regex is fine, but the regex.search always returns null even if the email introduced is correct, right now is like this but is not working as I said.

          regex.search(emailRegisterField.text.strip_edges()) == null:

          • xyz replied to this.

            Mario999 Post the exact code snippet (including the regex string) that causes the problem, in textual form please.

              xyz

              func _on_RegisterButton_pressed():
              	var usernameRegisterField = $RegisterElementsPanel/RegisterElementsContainer/UsernameRegisterLineEdit
              	var emailRegisterField = $RegisterElementsPanel/RegisterElementsContainer/EmailRegisterLineEdit
              	var passwordRegisterField = $RegisterElementsPanel/RegisterElementsContainer/PasswordRegisterLineEdit
              	var repeatPasswordRegisterField = $RegisterElementsPanel/RegisterElementsContainer/RepeatPasswordRegisterLineEdit
              	
              	var usernameRegisterMessage = $RegisterElementsPanel/RegisterElementsContainer/MessageWarningUsername
              	var emailRegisterMessage = $RegisterElementsPanel/RegisterElementsContainer/MessageWarningEmail
              	var passwordRegisterMessage = $RegisterElementsPanel/RegisterElementsContainer/MessageWarningPassword
              	var repeatPasswordRegisterMessage = $RegisterElementsPanel/RegisterElementsContainer/MessageWarningRepeatPassword
              	
              	var regex = RegEx.new()
              	regex.compile("^\\[\\w\\-\\.]+@[\\w\\-]+\\.[\\w\\-]{2,4}$")
              	
              	# Correct Data
              	
              	if usernameRegisterField.text.strip_edges().empty():
              		usernameRegisterMessage.text = "USERNAME_CANNOT_BE_EMPTY"
              		usernameRegisterMessage.visible = true
              	else:
              		usernameRegisterMessage.visible = false
              	
              	if emailRegisterField.text.strip_edges().empty():
              		emailRegisterMessage.text = "EMAIL_CANNOT_BE_EMPTY"
              		emailRegisterMessage.visible = true
              	
              	elif regex.search(emailRegisterField.text.strip_edges()) == null:
              		emailRegisterMessage.text = "EMAIL_INCORRECT"
              		emailRegisterMessage.visible = true
              	else:
              		emailRegisterMessage.visible = false
              • xyz replied to this.

                Mario999 Why do you escape the first square bracket?

                Shouldn't that regex string be just:

                "^[\\w\\-\\.]+@[\\w\\-]+\\.[\\w\\-]{2,4}$"

                  xyz

                  Yeeeep, I don't know when I added those \, even in the first message of the post you can see that they are not there, anyway, now it works as it should, thank you very much man!