Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking strings with regex in C++

Tags:

c++

string

regex

I am going to use regular expressions in a C++ application, but I am not experienced in regex. I want in particular to check a number of strings if they belong to one of the following categories:

X.anystring -> X must be necessarily and exclusively letter (not digit).

XY.anystring -> X, Y must be necessarily and exclusively digits 0-9 (not letters).

How can I check them using regex? What tutorial for regex could you recommend in order to acquaint me with regex?

like image 291
arjacsoh Avatar asked Dec 05 '25 20:12

arjacsoh


1 Answers

Seriously, regexp:s are not the right solution for you in this case.

To start with, regexp:s are not part of the C++ language so you will need to use a specific regexp library. (C++11, whoever, include support for regexp:s.)

Secondly, both your use cases can trivially be encoded in plain C++, all you need to do is to loop over the characters in the strings and check if each of them match your requirements.

like image 59
Lindydancer Avatar answered Dec 08 '25 10:12

Lindydancer



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!