Question marks in regular expressions

后端 未结 5 1017
难免孤独
难免孤独 2020-12-08 02:30

I\'m reading the regular expressions reference and I\'m thinking about ? and ?? characters. Could you explain me with some examples their usefulness? I don\'t understand the

5条回答
  •  -上瘾入骨i
    2020-12-08 03:07

    ? simply makes the previous item (character, character class, group) optional:

    colou?r
    

    matches "color" and "colour"

    (swimming )?pool
    

    matches "a pool" and "the swimming pool"

    ?? is the same, but it's also lazy, so the item will be excluded if at all possible. As those docs note, ?? is rare in practice. I have never used it.

提交回复
热议问题