0

Hi I just start learning on regex and got a question right away. I was playing around the expression and replacing the digits in a number "516.52" with a X.

So I tried the follows without a problem

\d{1} result: XXX.XX

\d{2} result: X6.X

\d{3} result: X.52

But why is this case?

\d{0} result: X5X1X6X.X5X2X

My original is that each digit in a number is evalued by the expression, and extend the digit when the expression has a quantifier. However, in the case of \d{0} , it seems that it would evaluate "the nothings" between/ before/ after the digits as well. May I ask how to understand the way the computer take regex expression and evaluate the data?

  • 2
    `\d{0}` will match between any characters (zero length), [same like `.{0}`](https://regex101.com/r/kcFrKn/1) - it does not make much sense though. – bobble bubble Sep 23 '22 at 17:19
  • Well `\d{0}` matches 0 repetitions of a digit, which is indeed nothing, and that can be found everywhere. – Bergi Sep 23 '22 at 17:19
  • Related: https://stackoverflow.com/questions/52369618/why-do-regex-engines-allow-automatically-attempt-matching-at-the-end-of-the-in https://stackoverflow.com/questions/24239573/x-quantifer-why-does-a-non-x-give-a-zero-length-match – Bergi Sep 23 '22 at 17:23
  • That indeed an interesting behaviour. So is it correct to said that: With an expression, the machine would evaluate each character and the null value between each of two characters? – Stanley Chan Sep 23 '22 at 23:04

0 Answers0