RegularExpressions in WPF

c# regex tutorial
regex c# tester
c regex cheat sheet
regular expressions
c# regex matches
c# regex ismatch
regular expression validation in c#
c# regex replace example

I know there are already some kind of posts, that try to explain the RegEx-string. but i still don't get it. In my case, I need a regex for a WPF, that only allows "Numeric-Keyboard". Its here in the Code:

private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
  Regex regex = new Regex("[-+]?[0-9]*,?[0-9]*");
  e.Handled = regex.IsMatch(e.Text);

so here are some example-numbers, i need to allow: "1" or "-1" or "-0,5" or "4,00" or "-3,56" or "3,3" or

" '-2, ' as '-2,0' or '-2' "

(so its all between -4 to +4. There can be a comma, But it don't have to be. If there s a comma, it needs one or 2 digits behind - not more. It should also be a "," not a "." - this is important) Does anybody know how to write this RegEx-String? thanks

Breaking it down, what you are after is not that complicated.

First off, your maximum/minimum range is from -4 up till 4. Taking into consideration the decimal section, you can have the following: ^[+-]?4(,0{1,2})?$. So in here, we expect a + or a - (optionally), the number 4, optionally followed by a comma and one or two 0's.

In your case, we now need to match the middle of your range, that is, from -3.99 up till 3.99. This can be achieved as follows: ^[+-]?[0-3](,\d{1,2})?$. In this case, we are also expecting a + or a - (optionally). We then expect to match a digit, between 0 and 3, optionally followed by a comma and 1 or 2 digits.

Combining them, we end up with something like so: ^[+-]?((4(,0{1,2})?)|([0-3](,\d{1,2})?))$. Example available here.

EDIT: As per the comments, you need to escape the slash in front of the \d, because the C# compiler will try and find a special meaning for \d, just like it does when you do \n, or \t. The easiest way is to use the @ character, so that the C# compiler threats the string as a literal: Regex regex = new Regex(@"^[+-]?((4(,0{1,2})?)|([0-3](,\d{1,2})?))$");.

RegularExpressions in WPF, Instead, use the RegEx mode which implements full functional regular expressions which give you more flexibility to control data input. Initializes a new instance of the Regex class by using serialized data. Regex(String) Initializes a new instance of the Regex class for the specified regular expression. Regex(String, RegexOptions) Initializes a new instance of the Regex class for the specified regular expression, with options that modify the pattern. Regex(String, RegexOptions, TimeSpan)

There are a number of online sites for testing regular expressions, that will break down the match string and provide an explanation for each element, e.g.

In your case, you need

optional '+' or '-'
    a group consisting of 
        optionally followed by a group consisting of 
            ',' and one or two '0'
    a group consisting of  
        one character from '0' .. '3'   
        optionally followed by a group consisting of 
            ',' and one or two characters from '0' to '9'


Mask Type: Simplified Regular Expressions, The following are code snippets for common regular expressions in C#. If you have a regular expression that you think is common or a  This article demonstrates how regular expressions can be used to validate user input in a Windows Presentation Foundation (WPF) application. The technique presented herein provides two ways for a developer to validate the text of a TextBox via regular expressions: explicitly adding a ValidationRule -derived object to the ValidationRules of a Binding , or simply using attached properties of a static service provider class.

string s = "-3,5;10;8.5;0;2;3.5;1,5";
string pattern = @"^-?[0-4](,\d{1,2})?$";
foreach(var num in s.Split(";"))
    Console.WriteLine($"Num: {num}, Matches: {Regex.IsMatch(num, pattern)}");

Regular Expressions, Use the Regex class and Regex.Match. Review features from System.Text.​RegularExpressions. using System; using System.Collections.Generic; using System.Text.RegularExpressions; namespace RegularExpressionsTest { class Program { static void Main(string[] args) { // Example 1 - Parameter=value // Match any character before and after the = // String thePattern = @"^.+=.+$"; // Match only Upper and Lowercase letters and numbers before // the = as a parameter name and after the equal match the // any character that is allowed in a file's full path // // ^[0-9a-zA-Z]+ This is any number

C# Regex.Match Examples: Regular Expressions, Regular expressions are a pattern matching standard for string parsing and replacement and is a way for a computer user to express how a  How can I set Regular Expression on TextBox? Ask Question Asked 10 years, 7 months ago. Browse other questions tagged wpf regex textbox or ask your own question.

Regular Expression In C#, Regular Expressions. A regular expression is used to check if a string matches a pattern or not. A regular expression or regex or regexp is a  Regular Expression Regular Expressions are specially formatted strings for specifying patterns in text. They can be useful during information validation, to ensure that data is in a specified format. For example, a ZIP code must consist of five digits, and the last name must start with a capital letter.

C# Regex Examples, C# - Regular Expressions - A regular expression is a pattern that could be matched against an input text. The .Net framework provides a regular expression​  Regular expressions provide a powerful, flexible, and efficient method for processing text. The extensive pattern-matching notation of regular expressions enables you to quickly parse large amounts of text to: Find specific character patterns. Validate text to ensure that it matches a predefined pattern (such as an email address).

  • Hey, thanks this sounds "easy" but: in Visual Studio there is a failure at ",\d" and it sais: CS1009 C# Unrecognized escape sequence... it happens everytime i added something with a backslash in regex... i don´t know why, i m even using System.Text.RegularExpressions;
  • The \ defines an escape sequence, try \\ or a verbatim string literal (@"regex")
  • with \\ at least there´s no error anymore, but it´s like: nothing happend, you can tip in everything you want:
  • Regex regex = new Regex("^[+-]?((4(,0{1,2})?)|([0-3](,\\d{1,2})?))$"); this way and Regex regex = new Regex("[^[+-]?((4(,0{1,2})?)|([0-3](,\\d{1,2})?))$]"); this way
  • @SKirchner: As i expected. Now consider Point 1. in my previous Answer: When you press a Key, this will be passed to your Event as "Input". This means your e.Text does only contain the pressed Key not the whole Text. So the regex matches only the number 4 but not the + - and therefore only accepts 4 to be entered. I suggest you to ask a new Question for this specific Problem, because we're drifting away from the original question.