0

Consider [-.\w]+.

I know [] means either or. I know, usually, . means any character (except a newline).

My initial thought was isn't it pointless to group . next to - and /w since . means anything (apart from new line)?

However, does . here literally mean a full-stop (or period if you're American) because a lot of characters don't need escaping in sets? And . is one of them?

P.s. If you do know the answer, would you mind sharing with me the resource that led to you knowing the answer?

tonitone120
  • 1,332
  • 1
  • 1
  • 16
  • This [Regex 101](https://regex101.com/r/pOJexe/1) Will describe for you exactly what each expression does and why. [This One](https://regex101.com/r/pOJexe/2) Includes search strings – Zak Aug 20 '20 at 20:40
  • 2
    `[]` doesn't mean either-or. It means any of the characters inside the brackets. – Barmar Aug 20 '20 at 20:47
  • That regexp means any sequence of letters, numbers, underscores, hyphens, and dots. – Barmar Aug 20 '20 at 20:47
  • Either-or is `either|or` – Barmar Aug 20 '20 at 20:48
  • Short answer: no. A dot inside square brackets is a dot. – Robo Robok Aug 20 '20 at 20:48
  • The only characters that need escaping inside square brackets are hyphen (except at the beginning), close bracket (except at the beginning), and backslash. – Barmar Aug 20 '20 at 20:50
  • @Barmar @Zak @RoboRobok Thanks all. If `.` literally just means any character except new-line, then why bother putting it next to `-` & `\w`? Surely `.` encompasses them? Unless I've misunderstood what `[]` does - I thought it meant: search for any (one) of the characters found here. – tonitone120 Aug 20 '20 at 20:58
  • 1
    `.` has no special meaning inside square brackets. – Barmar Aug 20 '20 at 20:59
  • So `[.]` means to match a literal dot. – Barmar Aug 20 '20 at 21:00
  • That's why it doesn't need escaping. – Barmar Aug 20 '20 at 21:00
  • I don't know why you removed the link in your question. That site explained what that regexp matches. – Barmar Aug 20 '20 at 21:01
  • **Any word can be the name, hyphens and dots are allowed. In regular expressions that’s `[-.\w]+`.** – Barmar Aug 20 '20 at 21:02
  • **`[-.]`** matches a single character in the list **`[-.]`** (case sensitive) – 0m3r Aug 21 '20 at 00:05
  • Okay, my take-away is that in `[]` `.` just means a a dot - not every character asides new line like it does outside `[]`. – tonitone120 Aug 21 '20 at 02:01

0 Answers0