regex

Ruby regex for extracting email addresses not detecting hypens


I tried looking at the regex that some others are using, but for some reason it's not working for me.

I just basically have a string, such as "testing-user@example.com", It'll only extract user@example.com and not the whole thing.

Here's what I have:

regex = Regexp.new(/\b[a-zA-Z0-9._%+-,]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/)
email = line.scan(regex)

Solution

  • The hyphen needs to be escaped for the position it is at inside of the character class.

    [a-zA-Z0-9._%+\-,]+
                  ^
    

    (+-,) currently matches a single character in the range between + and ,

    Inside of a character class the hyphen has special meaning. You can place the hyphen as the first or last character of the class. In some regex implementations, you can also place directly after a range. If you place the hyphen anywhere else you need to precede it with a backslash it in order to add it to your class.