Check octal number

I am writing a simple application in C ++ / Qt. And I have a text and an octal number. My app breaks this text with spaces. And I need to check for octal numbers from text. How can I select octal numbers from this text with regular expressions?

Thanks.

+2


a source to share


1 answer


You can use the following regex to match octal numbers only:

^0[1-7][0-7]*$

      

  • ^,$

    : Anchors
  • 0

    : Literal 0

    . Start all octal numbers with 0

    .
  • [1-7]

    : Char class for digits 1 through 7, since only they are valid octal digits.
  • *

    : Quantifier for zero or more.


So basically, this regex will only match lines that start 0

at the beginning and contain one or more digits from 1

to 7

.

If there is 0

no leading requirement , you can use a regular expression:

^[1-7][0-7]*$

      

+3


a source







All Articles