Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check octal number

Tags:

c++

regex

qt

I write simple application in C++/Qt. And i have a text and some octal number in it. My app splits this text by spaces. And i need to check octal numbers from text. How can i select octal numbers from this text with regular expressions?

Thank you.

like image 396
0xAX Avatar asked Sep 14 '26 09:09

0xAX


1 Answers

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

^0[1-7][0-7]*$
  • ^,$: Anchors
  • 0: A literal 0. All octal numbers begin with a 0.
  • [1-7]: Char class for digits from 1 to 7 as only these are valid octal digits.
  • * : Quantifier for zero or more of the previous thing.

So basically this regex matches only those strings that have a 0 in the beginning and contain one or more digits from 1 to 7.

If the leading 0 requirement is not there you can use the regex:

^[1-7][0-7]*$
like image 184
codaddict Avatar answered Sep 16 '26 23:09

codaddict



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!