Decyphering a simple regex

后端 未结 3 1932
失恋的感觉
失恋的感觉 2020-12-04 03:26

The regular expression in question is

(\\d{3,4}[.-]?)+

sample text

707-7019-789

My progress so far

<
相关标签:
3条回答
  • 2020-12-04 04:08

    The brackets remove the functionality of the dot. Brackets mean "Range"/"Character class". Thus you are saying Choose from the list/range/character class .- You aren't saying choose from the list "anything"- (anything is the regular meaning of .)

    0 讨论(0)
  • 2020-12-04 04:10
    [.-]?
    

    What this means literally:

    character class [.-]

    • Match only one out of the following characters: . and - literally.

    lazy quantifier ?

    • Repeat the last token between 0 and 1 times, as few times as possible.
    0 讨论(0)
  • 2020-12-04 04:17

    . in a character group [] is just a literal ., it does not have the special meaning "anything". [.-]? means "a dot or a hyphen or nothing", because the entire group is made optional with the ?.

    0 讨论(0)
提交回复
热议问题