Centimane
Centimane

Reputation: 280

Can regex be used with a character array in C++

I'm working on a program that cannot use the string library file, instead I am using char arrays. I am able to use regex, and was wondering if there is a way to use regex and character arrays, or even regex and a single char?

The reason why I ask is when I attempt to use my char array in a match the xUtility throws a bunch of errors from the "TEMPLATE CLASS iterator_traits"

if(regex_match(userCommand[3], userCommand[8], isNumeric))

errors:

Compiler error messages

Upvotes: 3

Views: 7637

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385274

std::regex_match and its friends work through iterators (as well as overloads for not only const std::string& but const char*).

So, yes, you can absolutely use a character array rather than std::string. I advise reading the documentation.


Per your edit:

if(regex_match(userCommand[3], userCommand[8], isNumeric))

If userCommand is the array, then you are passing in two chars, not pointers ("iterators").

Try:

if(regex_match(&userCommand[3], &userCommand[8], isNumeric))

or:

if(regex_match(userCommand + 3, userCommand + 8, isNumeric))

Upvotes: 5

Related Questions