1

Consider this simple example

http://time.com/this-time-is-different

Here I would like to match sequences such as this-time-is-different.

That is, any sequence of a word followed by a -, with this sequence being repeated at least three times. So in the example, the sequence word- is repeated three times only with this-time-is-different

However, when I use [-\w]{3,} I get too many matches, such as http which is not even followed by a -

enter image description here

What is wrong here? Thanks!

ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
  • 1
    `(\w+-){3,}\w+` a word followed by a hyphen 3 times or more followed by a word. [Demo](https://regexr.com/3un8f) – Oram Aug 30 '18 at 12:55
  • thanks @Oram, that is an interesting alternative. What is the difference with the `non-capturing-group` solution? – ℕʘʘḆḽḘ Aug 30 '18 at 13:03
  • 1
    It's a question of what you want to capture. Sometimes you don't want to have a lot of back references or you just want to optimize performance. `(?:\w+-){3,}\w+` is with a non-capturing-group. – Oram Aug 30 '18 at 13:17

1 Answers1

1

The [-\w]{3,} pattern is a character class matching a hyphen or a word char, three or more consecutive occurrences. So, it matches -----w--, wwd234_23--- like strings and more. You see, these chars may come in any order.

You may use a grouping construct here:

\w+(?:-\w+){2,}

See the regex demo. Here, (?:-\w+){2,} is a non-capturing group that matches 2 or more consecutive occurrences of a hyphen followed with one or more word chars.

Details

  • \w+ - one or more letters, digits or _
  • (?:-\w+){2,} - a non-capturing group matching 2 or more consecutive occurrences of
    • - - a hyphen
    • \w+ - one or more letters, digits or _.

If the - can appear consecutively, like a--b----d, you may apply the + after - to also match 1 or more occurrences:

\w+(?:-+\w+){2,}

See another regex demo.

Still, with the grouping construct, you ensure that -s should appear after at least 1 word char and they must be followed with at least 1 word chars.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563