0

I am new to RegEx and I want to use regular expression to find words between dots.

For example, the text is something like:

abc.efg.hij.klm.opq.

I tried with below RegEx:

\.(\w+)\.

It only show me 2 matches:

.efg.
.klm.

Why am I getting this result?

Here is the link to the RegEx: https://regex101.com/r/pqMN8t/1/

Tzar
  • 5,132
  • 4
  • 23
  • 57
wenesmad
  • 26
  • 2
  • do you need something more like "\.*\w+\."? – RoughTomato Jan 30 '19 at 07:41
  • For me, this is not a duplicate of the one marked above. Not exactly. Well, the reason is the same but the solution isn't. Sweeper's answer below has provided me the exact solution I was looking for. – wenesmad Jan 30 '19 at 08:00

1 Answers1

3

It only shows two matches because the regex engine will not match what it has already matched. After matching .efg., it won't match the dot before hij, because that dot has already been matched (the dot after efg).

One way to fix this is to not match the dots and use lookaheads and lookbehinds instead:

(?<=\.)\w+(?=\.)

This way, the dots won't get matched.

Sweeper
  • 213,210
  • 22
  • 193
  • 313