Home > Net >  How to compose a regex to find dot and numbers or nothing?
How to compose a regex to find dot and numbers or nothing?

Time:01-25

I need to find substrings of the following format:

.000<digits>

where the zeros' quantity may be from 0 (i.e., without zeros after the dot) to 3 and digits quantity starts from 0. But, if there is no the <digits> part, the regex should match nothing. I.e., I need to find the .123, .01, .0001, but not .000, ., .00001.

Is it possible to do this with regex?

UPDATE:

The <digits> part should be started with non-zero, but it may contain zero inside. Though, it's can be seen from the prev. text.

CodePudding user response:

You may use this regex:

\.0{0,3}[1-9]\d*$

RegEx Demo

This matches a dot followed by 0 to 3 zeroes followed by one non-zero digit and allows for 0 or more digits afterwards before end position.

  •  Tags:  
  • Related