Regular expressions, aka Regex, are very commonly used by any developer no matter what language he's writing his program. Today I'll show the most useful Regex that sooner or later you will need them.
1. URL Regex:
2. IP Address Regex:
3. Email Address Regex:
4. Username Regex:
5. Password Regex:
- Password length must be at least 8 characters.
- Password must contain at least one lower case letter, at least one upper case letter and at least one special character.
- Allowed aspecial characters: _!@#$%^&+=
6. Between Two Characters Regex:
Gets a string that is between two specific characters without including them, in our case these are two brackets.
[anything between these brackets]
no brackets here
7. File Extension Regex:
- Any file name with specific extension, in our case these are image files.
- Allowed extensions: jpg, png, gif or bmp
- You can change the allowed extensions in the regex on those what you need.
When you want to test your Regex I suggest to use Rubular online tool. It has user friendly interface and what I like the most in it is that it's showing the matching result on the fly.
And this is an example of how to use Regex in C#:
string regexPattern = @"([^\s]+(\.(?i)(jpg|png|gif|bmp))$)"; if (Regex.IsMatch(testString, regexPattern)) Console.WriteLine("This is an image file name."); else Console.WriteLine("This is not an image file name.");
Regex regex = new Regex(@"([^\s]+(\.(?i)(jpg|png|gif|bmp))$)"); Match match = regex.Match(testString); if (match.Success) Console.WriteLine(match.Groups.Value);