Regex

I have the following regex:

std::string s("AAA");
std::smatch m;
std::regex e("(?=.{3,}[A-Z])(?=.{0,}[a-z]).*");
output = std::regex_search(s, m, e);

Here it should have 3 or more uppercase letters and zero or more lowercase letter.
However the output is zero meaning it fails. When I try replace zero with 1 and
s("AAA4") it works fine.

So now I want it to allow zero or more but it seems like it is not accepting zero
I even tried quantifier (*) which is equivalent to {0,} still not working.
[A-Z]{3,}[a-z]*

[A-Z]{3,} any character in [A-Z], three or more repetitions

[a-z]* any character in [a-z], zero or more repetitions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <regex>

int main()
{
    const std::string text = "--ABA---CDDDExyz---FGHIJKLMNx---OPQRSTUVWwxyz---" ;

    const std::regex re( "[A-Z]{3,}[a-z]*" ) ;

    std::sregex_iterator iter( text.begin(), text.end(), re ) ;
    const std::sregex_iterator end ;
    for(  ; iter != end ; ++iter ) std::cout << iter->str() << '\n' ;

    std::cout << "-------------------\n" ;

    std::smatch match ;
    std::string str = text ;
    while( std::regex_search( str, match, re ) )
    {
        std::cout << match.str() << '\n' ;
        str = match.suffix() ;
    }
}

http://coliru.stacked-crooked.com/a/d9818c500e13cc14
Thanks for replying.
This regex works fine when lowercase/uppercase are consecutive, however in my case I want to find lowercase/uppercase regardless their position.

How can I do that using regex?
OP: with apologies, after reading and re-reading your posts I'm still finding it difficult to understand what it is that you're really trying to do. Could you please spell it out in a few simple sentences, possibly with an example? thanks
Last edited on
Here is an example:
string1 "AAA"
string2 "AAAb"
string3 "AbAA"

The following regex works with string1 and string2 as the uppercase are consecutive:
[A-Z]{3,}[a-z]*

The following regex works with string2 and string3 but it will not work when there are no lowercase even though I specified 0.
(?=.{3,}[A-Z])(?=.{0,}[a-z]).*

What I am looking for is a regex to work with all of them with following cases:
- Allow 0 or more occurrence of lowercase
- Validate 3 uppercase in string but they dont have to be consecutive like string 3
"([A-z]*[A-Z][A-z]*){3}"
This does work, thanks, but I need to consider lowercase too in regex as I send then number of occurrence as a input to function.
Allow 0 or more occurrence of lowercase

this condition will always be true
(?:[a-z]*[A-Z][a-z]*){3,}
pattern: a string made up solely of alpha characters, containing at least three upper case characters.
http://coliru.stacked-crooked.com/a/6771c8d3428cd2bf
Topic archived. No new replies allowed.