RegEx - Match Numbers of Variable Length

前端 未结 4 1898
小蘑菇
小蘑菇 2020-11-29 20:03

I\'m trying to parse a document that has reference numbers littered throughout it.

Text text text {4:2} more incredible text {4:3} much later on {22

4条回答
  •  庸人自扰
    2020-11-29 20:44

    You can specify how many times you want the previous item to match by using {min,max}.

    {[0-9]{1,3}:[0-9]{1,3}}
    

    Also, you can use \d for digits instead of [0-9] for most regex flavors:

    {\d{1,3}:\d{1,3}}
    

    You may also want to consider escaping the outer { and }, just to make it clear that they are not part of a repetition definition.

提交回复
热议问题