2

has anyone any idea why do I get full match of ".abcd" using the regex below on the string I posted? I imagine this regex to only capture ".abc" as a full match.

^(\.)([a-z]+){3}$

String: .abcd

Best regards:)

Luke Gmys
  • 137
  • 1
  • 7
  • I must clarify - I know how to solve that. I just want to know why it does not work as it is right now. – Luke Gmys Mar 08 '18 at 14:50
  • 1
    You're matching `ab`, then `c`, then `d`. This is because `+` is greedy, so it'll match as many as possible (`abcd`), but the regex still wants to be satisfied (hasn't yet satisfied `{3}`), so it backtracks to match `d` and then again to match `c` to satisfy `{3}` – ctwheels Mar 08 '18 at 14:51
  • @ctwheels Why does it match ab pair first? And then single letters? – Luke Gmys Mar 08 '18 at 14:51
  • See https://regex101.com/r/c1QpxI/1. – Wiktor Stribiżew Mar 08 '18 at 14:54

3 Answers3

5

The engine presumably works with your input as follows:

  • match the dot, fine
  • match [a-z]+ => abcd => first repetition
  • 2nd repetition => nothing => fail!
  • backtrack abcd => abc => 1st rep
  • match [a-z]+ => d => 2nd rep
  • 3rd rep => nothing => fail!
  • backtrack abc => ab => 1st rep
  • match cd => 2nd rep
  • 3rd rep => nothing => fail!
  • backtrack cd => c => 2nd rep
  • match d => 3rd rep
  • okay

In general, "quantified quantifiers" are very dangerous, because they involve lots of backtracking, see e.g. http://www.rexegg.com/regex-explosive-quantifiers.html

georg
  • 195,833
  • 46
  • 263
  • 351
0

You're currently saying start with a . then have at least 3 letters then end.

People, or at least I, suck at reading regex but here is a cool webpage that turns it into a flow chart for you https://regexper.com/#%5E%28%5C.%29%28%5Ba-z%5D%2B%29%7B3%7D%24

Image example

Andrew Bone
  • 6,549
  • 2
  • 15
  • 31
0
^\.[a-z]{3}$

This is the regex you need dot followed by three small letters

user2693928
  • 3,158
  • 1
  • 11
  • 19