C# Regex: Checking for "A-Z" and "A-Z"

C# Regex: Checking for a-z and A-Z

The right way would be like so:

private static bool isValid(String str)
{
return Regex.IsMatch(str, @"^[a-zA-Z]+$");
}

This code has the following benefits:

  • Using the static method instead of creating a new instance every time: The static method caches the regular expression
  • Fixed the regex. It now matches any string that consists of one or more of the characters a-z or A-Z. No other characters are allowed.
  • Much shorter and readable.

RegEx for matching A-Z, a-z, 0-9, _ and .

^[A-Za-z0-9_.]+$

From beginning until the end of the string, match one or more of these characters.

Edit:

Note that ^ and $ match the beginning and the end of a line. When multiline is enabled, this can mean that one line matches, but not the complete string.

Use \A for the beginning of the string, and \z for the end.

See for example: http://msdn.microsoft.com/en-us/library/h5181w5w(v=vs.110).aspx

How to check if a String contains any letter from a to z?

Replace your for loop by this :

errorCounter = Regex.Matches(yourstring,@"[a-zA-Z]").Count;

Remember to use Regex class, you have to using System.Text.RegularExpressions; in your import

Regex in c# with an AND

You can use a quantifier {12} to shorten the pattern, and use a positive lookahead to assert a digit, first optionally matching the same allowed characters of the character class without the digits.

^(?=[A-Za-z]*[0-9])[A-Za-z0-9]{12}$
  • ^ Start of string
  • (?=[A-Za-z]*[0-9]) Positive lookahead, assert optional chars A-Za-z to the right followed by a digit
  • [A-Za-z0-9]{12} Match 12 occurrences of either a char A-Za-z or a digit 0-9
  • $ End of string ( Or use \z)

You don't have to count the number of matches, you can return the value of IsMatch instead.

public static bool isUeiFormatOK(string test)
{
return new Regex("^(?=[A-Za-z]*[0-9])[A-Za-z0-9]{12}$").IsMatch(test);
}

Regex: A-Z characters only

use this regular expression (?i)^[a-z]+

Match match = Regex.Match(stringInput, @"(?i)^[a-z]+");

(?i) - ignore case

^ - begin of string

[a-z] - any latin letter

[a-z ] - any latin letter or space

+ - 1 or more previos symbol

Regex to allow only these [A-Z] / [a-z] / [0-9] / - / . / _ / ~

Your expression is as follows:

^[a-zA-Z0-9~\-_.]\S{42,128}$

Let's break it down into its components:

  • ^ - Matches the beginning of the input.
  • [a-zA-Z0-9~\-_.] - Match a single character a-z A-Z 0-9 ~ - _ or .
  • \S - Matches any character except a white-space character.
  • {42,128} - Matches previous element at least 42 times and at most 128 times. In this case, the previous element is \S.
  • $ - Matches the end of the input, or the point before a final \n at the end of the input.

Source for most definitions

So as we can see your regular expression currently does the following:

It first matches a single character a-z A-Z 0-9 ~ - _ or . at the start of the string. It then matches any non-whitespace character 42-128 times, and expects the end of the input to follow immediately afterwards. This means that we expect a string with a total length of 43-129 characters.

As your question states, you only want to match those specific characters, so your expression should actually be this (I've removed the \S):

^[a-zA-Z0-9~\-_.]{42,128}$

As for your specific input:

longenoughlongenoughlongenoughlongenoughlongenoughlongenoughlongenoughlongenoughAABSC__-......~~~.~-!

It appears that you have included an exclamation mark (!) at the end of your input, which of course doesn't match your constraint.

Regex for every letter in a string that appears once

You should prepend below regex to your regular expression:

(?!.*?([A-Z]).*\1)

But it should be just after caret ^. I'm going to break it down:

  • (?! Start of negative lookahead

    • .*? Lazy dot-star to expand matching lazily
    • ([A-Z]) Match and capture a capital letter between A and Z
    • .* Greedy dot-star to expand matching greedily (it could be lazy)
    • \1 Match whatever has been captured in previous capturing group
  • ) End of negative lookahead

and entire regex would be:

^(?!.*?([A-Z]).*\1)([A-Z]{2})([ ][A-Z]{2})*$

See live demo here

But be careful that this changes the order of your capturing groups since it adds one capturing group before all others (so if they were captured in 1 and 2 now they are 2 and 3). If you don't need to return them individually which means you don't need capturing groups then turn them to non-capturing groups:

^(?!.*?([A-Z]).*\1)[A-Z]{2}(?:[ ][A-Z]{2})*$

Because .NET supports infinite lookbehinds then a better approach would be utilizing this feature:

^[A-Z]{2}(?:[ ][A-Z]{2})*$(?<!\1.*([A-Z]).*?)

See live demo here

Regular expression to catch letters beyond a-z

You can use \pL to match any 'letter', which will support all letters in all languages. You can narrow it down to specific languages using 'named blocks'. More information can be found on the Character Classes documentation on MSDN.

My recommendation would be to put the regular expression (or at least the "letter" part) into a localised resource, which you can then pull out based on the current locale and form into the larger pattern.

Regex to select *.aspx until something different from a-z

While you could certainly use regular expression to handle this, you might consider some of the built-in functions within .NET that can already handle this for you. I'll provide two examples for how to resolve this (with and without regular expressions).

Using The Path.GetFileNameWithoutExtension() Method

The System.IO namespace actually exposes a method called GetFileNameWithoutExtension() that will handle this exact operation for you :

// This will find the file mentioned in the path and return it without
// an extension ("pgs/ChangePassword.aspx?a=5" > "ChangePassword")
var fileName = Path.GetFileNameWithoutExtension(input);

You can see this approach here.

Via a Regular Expression

You can accomplish this through a lookahead which will match a string of one or more letters [a-zA-Z]+ that precede an .aspx using the Regex.Match() method:

// This will match any set of alphabetical characters that appear before
// an '.aspx' ("pgs/ChangePassword.aspx?a=5" > "ChangePassword")
var fileName = Regex.Match(input,@"[a-zA-Z]+(?=\.aspx)");

You can see a working example here.



Related Topics



Leave a reply



Submit