Check if an uppercase Letter is inside a string

2019-09-12 14:19发布

问题:

I am currently learning C# and RegEx. I am working on a small wordcrawler. I get a big list of many words where I escape those which don't fit to my RegEx.

Here's my code:

var WordRegex = new Regex("^[a-zA-Z]{4,}$", RegexOptions.Compiled | RegexOptions.CultureInvariant);
var secondRegex = new Regex("([A-Z]{1})");

var words = new List<string>();
var finalList = new List<string>();

foreach (var word in words)
{
    if (WordRegex.IsMatch(word) && secondRegex.Matches(word).Count == 1 || secondRegex.Matches(word).Count == 0)
    {
         finalList.Add(word);
    }
}

So this works fine if the word is 'McLaren' (two uppercase letters) it won't add it to finalList. But if the words is something like 'stackOverflow' (one uppercase letter but not at start of string), it does take it to finallist. Is there any simple way to prevent this problem?

PS: if there is any better way than RegEx let me know!

Here are some examples:

("McLaren");//false
("Nissan");//true
("BMW");//false
("Subaru");//true
("Maserati");//true
("Mercedes Benz");//false
("Volkswagen");//true
("audi");//true
("Alfa Romeo");//false
("rollsRoyce");//false
("drive");//true

These with true should be accepted and the other shouldn't be accepted.

What I want to reach is that the regex shouldnt add when its written like this'rollsRoyce' but if it's written like 'Rollsroyce' or 'RollsRoyce' it should be accepted. So I have to check if there are uppercase letters inside the string.

回答1:

There is a very easy solution without regex or linq:

bool hasUppercase = !str.equals(str.toLowerCase());

Now you can easily check:

if(!hasUppercase) {
    // no uppercase letter
}

else {
    // there is an uppercase letter
}

Just checking if the string is equal to its lowercased self.



回答2:

If you want to check if the string contains an upper letter - this would be my approach

string sValue = "stackOverflow";
bool result = !sValue.Any(x => char.IsUpper(x));

Update to the updated question

string sValue = "stackOverflow";
bool result = sValue.Where(char.IsUpper).Skip(1).Any();

this ignores the 1st char and determines if the rest of the string contains at least one upper letter