Regex to restrict UPPERCASE only
Asked Answered
R

5

7

I am stil struggling with understanding how Regex works exactly.

I am getting usernames and they are in this format:

firstname.lastname

Both names may contain special international characters and they may contain an ' or - but I just need to detect if they contain any uppercase letters so I can throw an exception.

I am using this expression

[^A-Z].[^A-Z]

It seems to me that this should work, I just don't understand why it doesn't.

I hope somebody could explain.

Thanks!

Riband answered 25/7, 2013 at 14:45 Comment(7)
dot has a special meaning .. #5697421Washable
you can drop Regex and simply use if(yourString.Any(char.IsUpper)) {...}Award
you should do what @w0lf saidDepict
I am trying to use Regex for all of my validations, becasue there are some cases that I have to use regex and I also need to learn it :)Riband
[^A-Z] is only matching 1 character not uppercase and like it was said above '.' Is actually matching any character except newlines. You probably want to escape it '\.'Homeroom
the ^ tag means not a uppercase letter not an uppercase letter @dasblinkenlightBefore
@user2247823 Regex is better because yo can validate the format and that it doesn't have a capital letter.Before
B
11

[^A-Z] Simply means any character that isn't a capital A through capital Z.

. Means any character you should be using \. As this means the literal character .

A character group is [] and the inverse is [^] you then put the characters you want to match.

However, your regex looks like it will match only a single character that isn't a capital letter then any character then another single character that isn't a capital letter

You want to use the following:

[^A-Z]+\.[^A-Z]+

The + in regex means match the before stated 1 to infinite times.

If you are only going to have this text and no other text you should include the start of line and end of line tag so that it doesn't match long strings that include something formatted like you mentioned.

However, your regex does also match spaces and tabs.

So I would use the following:

^[^A-Z\s]+\.[^A-Z\s]+$

Regex Demo working with only lowercase

Regex Demo failing because username has uppercase letter

Before answered 25/7, 2013 at 14:51 Comment(5)
Almost, just add the escape you mentioned \.Bedpost
lol, believe it or not i had a \. but the <code> tag made it .Before
I had to escape it by using \\. and \\s, is this a C# thing? it complains if I use a single \ but it works for a small test case that I haveRiband
@user2247823 yes or you can do this @"^[^A-Z\s]+\.[^A-Z\s]+$" which allows the escape character without escaping \ is an escape character in c#. that's why you need \\ that's fine to keep like thatBefore
@user2247823 If you're using \ in a string, then it's probably complaining about not recognizing the escape sequence. Using \\ tells the C# compiler that you want a literal \ in your string.Godfry
V
4

Instead of using regex you could use this method to check for upper case characters.

public static bool checkStringForUpperCase(string s) 
{
    for (int i = 0; i < s.Length; i++)
    {
        if (char.IsUpper(s[i]))
            return false;
    }
    return true;
}
Valais answered 25/7, 2013 at 14:52 Comment(4)
Not sure why I would get voted down when I was just offering an alternative solution to which someone may find useful if they're in the same situation.Valais
The only issue I see with this is it doesn't do any validation other than UPPERCASE. So it doesn't even check for the period in the middle and will allow for white space as a space isn't capital.Before
That is true, although you may also use the string.contains to see if those are included within the string.Valais
that would only verify that one or more periods are in the string in any place.Before
G
4

Obviously the only correct answer is to use \p{Lu} to match an uppercase Unicode letter. There are other uppercase letters in national alphabets other than [A-Z].

Greenland answered 10/9, 2019 at 18:12 Comment(0)
S
3

If you want to check that there is no uppercase, you don't need dot int middle, you can use just [^A-Z] You should use start and end regex symbols and sign that this can be more then one symbol. If i remember correctly it should be something like ^[^A-Z]*$

Smyth answered 25/7, 2013 at 14:50 Comment(0)
M
0

If you only want to check whether it contains uppercase or not. Try this.

  string test = @"Test";
  string test2 = "test";
  bool result = test.Any(x=>char.IsUpper(x));  //true
  result = test2.Any(x => char.IsUpper(x));//false
Mattins answered 25/7, 2013 at 14:53 Comment(4)
@downvoter can you please comment why you downvoted? what is wrong with the answer?Mattins
i have not downvoted but would your code work! char.IsUpper should be x=>char.IsUpper(x)..Aksi @ is unnecessary..Spinous
The only issue I see with this is it doesn't do any validation other than UPPERCASE. So it doesn't even check for the period in the middle and will allow for white space as a space isn't capital.Before
@Before agree. that is why i wrote "If you only want to check whether it contains uppercase "Mattins

© 2022 - 2024 — McMap. All rights reserved.