This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 8 years ago.
Im working on an assignment for class and it includes using some checks for input to ensure valid input is given, using a regex expression. The example given for the IsValidNumber check is string regex = #"^-?\\d*(\\.\\d+)?$";
I'm not really sure if this is actually checking for valid numbers because i've never seen that before. Wouldn't something like string regex = #"^[0-9]\d*$ work or am i missing something? Note I do need to be able to pass through a float and not just an int.
UPDATE:
Using the regex I was provided, i ran the program only to run into an issue with the error check that didn't allow anything to pass through even if valid
Here is the method that is calling IsNumberValid
private static string GetNumericInput(string c)
{
do
{
Console.Write(c);
string var = Console.ReadLine();
if (!String.IsNullOrEmpty(var) && IsValidNumber(var))
{
return var;
}
else
{
Console.WriteLine("Input was either Null/Empty, or contained non-numeric characters.");
Console.WriteLine("Retry input.");
Console.WriteLine();
}
}
while (true);
}
and here is IsValidNumber
public static bool IsValidNumber(string value)
{
string regex = #"^-?\\d*(\\.\\d+)?$";
return Regex.IsMatch(value, regex);
}
I'm not entirely sure how to read breakpoints for errors but when i put breakpoints at string regex = #"^-?\\d*(\\.\\d+)?$"; and return Regex.IsMatch(value, regex); it shows value for regex as null and "^-?\\d*(\\.\\d+)?$" respectively.
The regex is taking into consideration the decimal point, i.e. it matches not just integers but also floating point numbers. This is what the \\. catches. Also the ^-? matches a possible negative sign. ^[0-9]\d* only matches positive integers.
The statement string regex = #"^-?\\d*(\\.\\d+)?$"; creates a regex pattern that's literally ^-?\\d*(\\.\\d+)?$ because of the # at the front of the string. You don't need to use double backslashes in this case, simply:
string regex = #"^-?\d*(\.\d+)?$";
In addition to #manouti answer of this regex takes in consideration floating point numbers as well, it also takes in account for negative numbers
Related
This question already has answers here:
Case insensitive 'Contains(string)'
(29 answers)
Closed 3 years ago.
I tried several solutions from the internet an worked myself through some tutorials but I am not able to make it work. I try to match a word in a string with random letters, numbers or dots before and/or after.
eg.
Meeting.Room
MeetingRoom21
Room
Meeting2Room
Meeting.room
12MeetingRoom110.MeetingRoom
I try to match the word "Room" but it should not be case sensitive.
The last pattern I tried was this: \b()(\wRoom\w)\b \ig
But I use regex not that much and I struggle to solve something after three months.
I hope someone can help me.
public bool Regex_check_for_match(string input, string pattern)
{
bool ismatch = Regex.IsMatch(input, pattern);
return ismatch;
}
So you want to see if the input contains the word room? In which case there is no need for regex, just do a simple index check
return culture.CompareInfo.IndexOf(input, "room", CompareOptions.IgnoreCase) >= 0
Where culture is the instance of CultureInfo.
Why use a regex? Just use .Contains...
public bool CheckForMatch(string input, string pattern)
{
return input.Contains(pattern, StringComparison.OrdinalIgnoreCase);
}
My guess is that maybe you might want to design an expression to find room in different settings, such as:
\B(room)\B|\b(room)\b|(room)
Example
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = #"\B(room)\B|\b(room)\b|(room)";
string input = #"Meeting.Room
MeetingRoom21
Room
Meeting2Room
Meeting.room
12MeetingRoom110.MeetingRoom";
RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
The expression is explained on the top right panel of this demo, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs step by step, if you like.
FYI To match numerics, use '\d' and for non-alpha characters use '\W', to define a matching group use parentheses () and square brackets for optional matches
Using http://regexstorm.net/tester I created a regex ... but I'm confused why you would need to that when you could merely use indexOf instead
To ignore the prefacing elements you can use
[\d\W]*(Meeting)?(Room)
I have a windows form application that takes this regex:
public bool priceRegex (string parameter)
{
Regex regexPrice = new Regex(#"^[0-9]+(\.[0-9][0-9]?)?");
Match matchPrice = regexPrice.Match(parameter);
return matchPrice.Success;
}
And uses it here:
if (name.priceRegex(PriceEntryBox.Text) == false)
{
MessageBox.Show("Invalid characters entered in Price Box", "Error");
}
Which is followed by an else which continues with other code.
This works, and the Message box shows when I type in "fdsf" or "dasds123", but it throws an exception when ever "12fsdf" is typed in, instead of showing the message box. Any help?
Your old regular expression was only checking to see if the the entered value started with something valid (which as it turns out, anything that began with a number was valid with your regular expression). If you want to make sure the whole thing matches the regular expression, you must begin with ^ and end with $, not just begin with ^.
Regex regexPrice = new Regex(#"^[0-9]+(\.[0-9][0-9]?)?$");
It is the equivalent to the following as well:
Regex regexPrice = new Regex(#"^[0-9]+(\.[0-9]{1,2})?$");
Match method searches the specified input string for the first occurrence of the regular expression specified in the Regex. So '12fsdf' matches 12 in provided input and your method returns Success value. So instead of using Match, you should use IsMatch method of Regex which indicates whether the specified regular expression finds a match in the specified input string.
public bool priceRegex (string parameter)
{
Regex regexPrice = new Regex(#"^[0-9]+(\.[0-9][0-9]?)?");
return regexPrice.IsMatch(parameter);
}
This simple regular expression matches the text of Movie. Am I wrong in reading this as "Q repeated zero or more times"? Why does it match, shouldn't it return false?
public class Program
{
private static void Main(string[] args)
{
Regex regex = new Regex("Q*");
string input = "Movie";
if (regex.IsMatch(input))
{
Console.WriteLine("Yup.");
}
else
{
Console.WriteLine("Nope.");
}
}
}
As you are saying correctly, it means “Q repeated zero or more times”. I this case, it’s zero times, so you are essentially trying to match "" in your input string. As IsMatch doesn’t care where it matches, it can match the empty string anywhere within your input string, so it returns true.
If you want to make sure that the whole input string has to match, you can add ^ and $: "^Q*$".
Regex regex = new Regex("^Q*$");
Console.WriteLine(regex.IsMatch("Movie")); // false
Console.WriteLine(regex.IsMatch("QQQ")); // true
Console.WriteLine(regex.IsMatch("")); // true
You are right in reading this regex as Q repeated 0 or more times. The thing with that is the 0. When you try a regex, it will try to find any successful match.
The only way for the regex to match the string is to try matching an empty string (0 times), which appears anywhere in-between the matches, and if you didn't know that before, yes, regex can match empty strings between characters. You can try:
(Q*)
To get a capture group and use .Matches and Groups[1].Value to see what has been captured. You'll see that it's an empty string.
Usually, if you want to check the existence of a character, you don't use regex, but use .Contains. Otherwise, if you do want to use regex, you'd drop the quantifier, or use one which matches at least one particular character.
I have had a difficult time wrapping my head around regular expressions. In the following code, I used a Regex to determine if the data passed was a 1 to 3 digit number. The expression worked if the data started with a number (ex. "200"), but also passed if the data had a letter not in the first digit (ex. "3A5"). I managed to handle the error with the INT32.TryParse() method, but it seems there should be an easier way.
if (LSK == MainWindow.LSK6R)
{
int ci;
int length = SP_Command.Length;
if (length > 3) return MainWindow.ENTRY_OUT_OF_RANGE; //Cannot be greater than 999
String pattern = #"[0-9]{1,3}"; //RegEx pattern for 1 to 3 digit number
if (Regex.IsMatch(SP_Command, pattern)) //Does not check for ^A-Z. See below.
{
bool test = Int32.TryParse(SP_Command, out ci); //Trying to parse A-Z. Only if
if (test) //it no letter will it succeed
{
FlightPlan.CostIndex = ci; //Update the flightplan CI
CI.Text = ci.ToString(); //Update the Init page
}
else return MainWindow.FORMAT_ERROR; //It contained a letter
}
else return MainWindow.FORMAT_ERROR; //It didn't fit the RegEx
}
Regex.IsMatch searches the input string for the pattern (and thus returns true for 3A5 because it finds 3).
You should also include start (^) and end ($) of string:
String pattern = #"^[0-9]{1,3}$";
Adding line begin/end should help.
^[0-9]{1,3}$
I tried to write an expression to validate the following pattern:
digit[0-9] at 1 time exactly
"dot"
digit[0-9] 1-2 times
"dot"
digit[0-9] 1-3 times
"dot"
digit[0-9] 1-3 times or “hyphen”
For example these are legal numbers:
1.10.23.5
1.10.23.-
these aren't:
10.10.23.5
1.254.25.3
I used RegexBuddy to write the next pattern:
[0-9]\.[0-9]{1,2}\.[0-9]{1,3}\.[0-9]{1,3}|[0-9]\.[0-9]{1,2}\.[0-9]{1,3}\.-
In RegexBuddy all seems perfect but in my code I am getting true about illegal numbers (like 10.1.1.1)
I wrote the next method for validating this pattern:
public static bool IsVaildEc(string ec)
{
try
{
if (String.IsNullOrEmpty(ec))
return false;
string pattern = #"[0-9]\.[0-9]{1,2}\.[0-9]{1,3}\.[0-9]{1,3}|[0-9]\.[0-9]{1,2}\.[0-9]{1,3}\.-";
Regex check = new Regex(pattern);
return check.IsMatch(ec);
}
catch (Exception ex)
{
//logger
}
}
What am I doing wrong?
You regex isn't anchored to the start and end of the string, therefore it also matches a substring (e. g. 0.1.1.1 in the string 10.1.1.1).
As you can see, RegexBuddy matches a substring in the first "illegal" number. It correctly fails to match the second number because the three digits in the second octet can't be matched at all:
string pattern = #"^(?:[0-9]\.[0-9]{1,2}\.[0-9]{1,3}\.[0-9]{1,3}|[0-9]\.[0-9]{1,2}\.[0-9]{1,3}\.-)$";
will fix that problem.
Then, your regex is needlessly complicated. The following does the same but simpler:
string pattern = #"^[0-9]\.[0-9]{1,2}\.[0-9]{1,3}\.(?:[0-9]{1,3}|-)$";
try:
#"^[0-9]\.[0-9]{1,2}\.[0-9]{1,3}\.[0-9]{1,3}|[0-9]\.[0-9]{1,2}\.[0-9]{1,3}\.-"
you are not starting from the beggining of the text
If you match against the "10.1.1.1" the "0.1.1.1" part of your string would be a correct number and therefor return true.
Matching against
#"^[0-9]\.[0-9]{1,2}\.[0-9]{1,3}\.[0-9]{1,3}|[0-9]\.[0-9]{1,2}\.[0-9]{1,3}\.-"
with the ^ sign at the beginning means that you want to match from the beginning.
You are missing the ^ char in the start of the regex.
Try this regex:
^[0-9]\.[0-9]{1,2}\.[0-9]{1,3}\.[0-9]{1,3}|[0-9]\.[0-9]{1,2}\.[0-9]{1,3}\.-
This C# Regex Cheat Sheet can be handy