I tried to use the following regular expression, which already works in C#
, in C++
as well but it\'s not working in C++
.
This is similar sln's, but it's shorter and doesn't require a %
part to match:
^(?:[^%]*(?:%\.?[0-9]*[a-z])?)*$
First - everything between ^
(start of line) and $
(end of line) is optional, so an empty string is accepted.
In an optional, non capturing group (?:...)
, match any number of anything but a %
. Then, optionally, match a %
, optionally followed by a .
, and then any number of digits and finally a letter. Repeat this for any number of times.
(I, as the others answering, and as the regex provided in the question suggests, assume that OP doesn't mean "immediately preceded by a letter", but instead followed by one, right?)
See it here at regex101.
Here you go, all %
in string must be compliant.
If so, match the entire string, if not, don't match
the string.
I suggest you do this with i.e. if ( regex_search( sTarget, sRx, sMatch, flags ) )
but regex_match() would do the same thing.
^(?:[^%]*%(?:\.[0-9]*)?[a-z])+[^%]*$
Expanded
^ # BOS
(?: # Cluster begin
[^%]* # Not % characters
% # % found
(?: \. [0-9]* )? # optional .###
[a-z] # single a-z required
)+ # Cluster end, 1 to many times
[^%]* # Not % characters
$ # EOS
A simple regular expression that works with VC++ and fits your description would be
std::regex("^([^%]|%(\\.[0-9]+)?[a-zA-Z])*$", std::regex::extended)
Live demo
(Change [0-9]+
to [0-9]*
if digits after %.
should be optional).