How to match repeated patterns?

后端 未结 4 1930
我在风中等你
我在风中等你 2020-12-08 14:59

I would like to match:

some.name.separated.by.dots

But I don\'t have any idea how.

I can match a single part like this

         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 15:17

    You can use ? to match 0 or 1 of the preceeding parts, * to match 0 to any amount of the preceeding parts, and + to match at least one of the preceeding parts.

    So (\w\.)? will match w. and a blank, (\w\.)* will match r.2.5.3.1.s.r.g.s. and a blank, and (\w\.)+ will match any of the above but not a blank.

    If you want to match something like your example, you'll need to do (\w+\.)+, which means 'match at least one non whitespace, then a period, and match at least one of these'.

提交回复
热议问题