3

I'm new in PHP and I'm trying to accommodate php regular expression

from w3schools this regular expression

"/([\w\-]+\@[\w\-]+\.[\w\-]+)/"

represents the e-mail regular expression, but I'm wondering what this class definition means "[\w\-]", \w any word character but what about "\-"?

(Edited)

Aladdin
  • 923
  • 1
  • 13
  • 25
  • possible duplicate of [Reference - What does this regex mean?](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – HamZa Jul 19 '14 at 16:49
  • 1
    The regex you have merely represents a "valid" e-mail address. For example, it fails for `foo+bar@baz.qux`. The character class `[\w\-]` will match a word character and a hyphen `-` literally. That said, you don't need to escape a hyphen at the end of a character class, so `[\w-]` is perfectly valid. Also escaping `@` is useless. On a general note, you should avoid w3schools as a source for learning, it has a bad history of outdated, sometimes misleading information. Get a book instead. – HamZa Jul 19 '14 at 16:52
  • can you provide me a good book to learn PHP briefly? – Aladdin Jul 19 '14 at 17:20

4 Answers4

6

But what about \- ?

The hyphen is mostly a normal character in regular expressions.

  • Outside of a character class; it has no special meaning (do not need to escape the hyphen).
  • Inside of a character class it has special meaning.

You can place a hyphen as the first or last character of the class without escaping.

  • ( [-\w], [\w-] )

In some regular expression implementations, you can also place directly after a range without escaping.

Simply escaping the hyphen as the last character of the class is fine here.

[\w\-]  # any character of: word characters (a-z, A-Z, 0-9, _), 
        # match a literal hyphen `\-'
hwnd
  • 65,661
  • 4
  • 77
  • 114
4

[\w\-] means letters(capital and small letter both) and numbers including -(dash/hypen)

You may check your regex explanation here

[\w\-]+ match a single character present in the list below

Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]

\w match any word character [a-zA-Z0-9_]

\- matches the character - literally

pasja
  • 355
  • 4
  • 10
Rahul Tripathi
  • 152,732
  • 28
  • 233
  • 299
  • you might forget underscore – Braj Jul 19 '14 at 16:20
  • OP is talking about `[\w\\-]` – Braj Jul 19 '14 at 16:28
  • @R.T. Please do not just copy and paste from an automated script. [Here's something you might want to read](http://meta.stackoverflow.com/questions/252868/regex-reference-and-its-fate) – HamZa Jul 19 '14 at 16:54
  • @HamZa:- I do understand that but first I have explained that and then I have added the link and I dont think that this is a wrong explanation and I deserve the downvote(Downvotes are awarded for wrong or incorrect answers). Anyways its your call and I dont want to discuss it further. Thanks! – Rahul Tripathi Jul 19 '14 at 16:59
  • @R.T. That said, the OP is using `[\w\-]` while you're explaining `[\w\\-]` which are slighty different regexes. The OP might get confused :) – HamZa Jul 19 '14 at 17:00
  • @HamZa:- Yes I saw that. OP firstly used `[\w\\-]` then I think he has changed that! – Rahul Tripathi Jul 19 '14 at 17:01
  • 1
    @R.T. Ok, I see the "problem". The OP has used a double backslash because of SO's markdown, it showed one backslash correctly. But then @Braj went and added the backticks to "codify" the regex. So markdown will display the double backslash literally. From a logical point of view, it should be `[\w\-]` but you never know with those [fools](http://www.w3fools.com) – HamZa Jul 19 '14 at 17:04
  • 1
    @HamZa:- Yes I agree. I have updated my answer. Just to make my answer correct. One more edit to question will ask me to delete my answer. As I am really sick of modifying the answer and getting unnecessary downvotes because of irregular edits – Rahul Tripathi Jul 19 '14 at 17:05
1

All [\w\-] Means is match and word character \w and match any - \-

it means the exact same thing as [\w-]

Fletcher Ripp
  • 1,157
  • 10
  • 18
0

\w matches any word character plus _, underscore, i.e. upper and lowercase alphabets, numbers 0 to 9 and underscore.
\- matches a normal dash.

so [\w\-] would allow numbers, alphabets, underscores and dashes in the email alias.
(it should be noted that \., which allows a dot, is not included for the regex before @ although, .s are allowed for email aliases)

sunbabaphu
  • 1,453
  • 1
  • 10
  • 15