0

I have a std::string, with a text as such

name0 0x3f700000 0x160000 1 0

or

name1 0x3f700000 0x760000 0 23

etc..

What I would like to know is if the last number in this line is greater than 0 and the number before is 1.

I did this but it doesn't work, always it returns a match.

std::regex_search(buffer, match, std::regex(std::string("(^|\n)") +  
m_name + " [0-9a-fA-Fx]* [0-9a-fA-Fx]* 1 [1-9a-fA-Fx]*"));

Can you say where the error is? It seems to know when the number before is 1 but that last number seems to be going wrong.

user1876942
  • 1,411
  • 2
  • 20
  • 32

2 Answers2

1

You can use

[1-9][0-9]*$ 

to check if the number is greater than 0

What it does?

  • [1-9] Matches 1 to 9.

  • [0-9]* Matches zero or more digits

  • $ Matches end of string.

The full regex can be

name0 [0-9a-fA-Fx]* [0-9a-fA-Fx]* 1 [1-9][0-9]*$

Regex Demo

nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52
1

You can use this regex

1\s+[1-9]\d*$

Regex Demo

or more specifically

^name\d+\s+0x[0-9a-f]+\s+0x[0-9a-f]+\s+1\s+[1-9]\d*$

Regex Demo

rock321987
  • 10,942
  • 1
  • 30
  • 43