-2

I understand . matches a single character. So .* should mean any character , 0 or more times.

So it should match aaaaaaaaaa (a 10 times), bbbbbbbbb (b 9 times), '{nothing here}' (any character 0 times)

But why does it match abasdfasdfadv (Because I dont see 0 or more occurence of any character)

Rpant
  • 974
  • 2
  • 14
  • 37
  • See https://stackoverflow.com/a/12666777/3832970 – Wiktor Stribiżew Mar 16 '18 at 20:33
  • 1
    @Rpant also checkout this excellent explanation: https://www.regular-expressions.info/dot.html – Bart Kiers Mar 16 '18 at 20:48
  • @BartKiers Thanks , that link helped understand. "The dot matches any character, and the star allows the dot to be repeated any number of times" ,So basically we are repeating . as many times as we can , and then dot matches any character , Rather than match dot with one character and then expect the character to be repeated. – Rpant Mar 16 '18 at 21:02

1 Answers1

0

The .* means "zero or more of any character", not "the same character repeating zero or more times".

Imagine . is approximately like [0x00-0xFF], or in other words, any character is acceptable.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 1
    In most regex implementations, `.` excludes line breaks. So, _almost_ any character is acceptable :) – Bart Kiers Mar 16 '18 at 20:43
  • Good point, and I was going to explain that but then it gets really complicated. "Approximate" is the idea here. – tadman Mar 16 '18 at 20:43
  • Yes, it might be useful to point out that detail, but in any case I think this answer addresses the question directly in a way that the answers to the "duplicate" question don't. – David K Mar 16 '18 at 20:46
  • I have observed that behavior and saw that answer too before posting , but something doesn't look right. Imagine trump saying i will do something 3 times. You wouldn't expect him to do 3 different things. (Altough he would). When you say anything will be repeated 0 or more times , you expect 1 particular thing to get repeated. – Rpant Mar 16 '18 at 20:50
  • @Rpant The key here is *you* expect that, but that's not how regular expressions have ever worked, so you need to adjust your expectations. It may not make sense until you think of it in terms of how a regular expression thinks about it, and then you can start to use them more effectively. – tadman Mar 16 '18 at 21:11