Backslash in Regular Expression

Asked

Viewed 280 times

3

was studying java and the course instructor left a challenge to validate an email using regular expressions.

I searched the regex API documentation...for the Pattern part.. The problem is that I found a code on the internet (which worked, obvious).. so:

String regex = "[A-Za-z0-9\\._-]+@[A-Za-z0-9]+(\\.[A-Za-z]+)*";

In this case, I didn’t understand just why he put the characters "[" and "]", "(" and ")" as well as the " " (which I know means the backslash, but I didn’t understand why it was used in this code.

Does anyone know?

  • The backslash is the kryptonite of regular expression. But first there is the Java string interpretation, so to have a backslash in regex (to, say, neutralize the powers of .), in Java is written \\.

  • 2

    The double backslash is to escape it. The list of the first part is limiting alphanumerics, including the dot, underline and hyphen, but point is also a regex character, then you need to escape it with a backslash. But java also recognizes the backslash as an escape, so you need to escape the java bar to then escape the point within the regular expression.

  • Perfect! Thank you very much staff!

1 answer

3


[]: It is used to match any character inside.

Example: [A-Z] matches the characters from A to Z (note that only uppercase)

(): It is used to create a capture group, to extract substrings or to use as a reference.

Example: (.[A-Za-z]+)* will match the possible Tlds and the * is a quantifier of 0 or more

The use of \\ in the expression given was for the item to be treated as the character .

If you want to test regular expressions quickly and learn more about their use, I recommend using the Regexr

As remembered by Jefferson Quesado in the commentary, the use of two \ instead of one if you notice the fact that Java does not support "raw" strings, like this \\. is interpreted as \.

  • 1

    All I could say was \\ is a consequence of Java not having the possibility to insert raw strings.

  • 1

    Well noticed, I forgot to comment on, I will add in the reply

  • ... and that actually doesn’t need to escape the point within []s

Browser other questions tagged

You are not signed in. Login or sign up in order to post.