First, I wanna ask what is the difference between Regex.IsMatch and Regex.Match ?
Second, if I want to figure it out myself where should I go to? I mean, is there any help in c# to see what type does a method accept as input or how does it work at all?!!
Third, I wanna check the input to see if it contains only numbers.
And last, is there any website out there to give me regular expressions? It is a big story! I cannot memorize all the patterns and syntax!
Please dont try with Int32.TryParse() ! I wanna do it with Regex.
I tried this lines of code below:
string input = Int32.Parse(ConsoleReadLine().Trim());
public bool Validation (int myInt)
{
return Regex.Match(myInt, #"^[0 - 9] *$");
}
Here is the Windows Documentation for the Regex Class. IsMatch returns a boolean if the string matches the conditions. Match returns an instance of the RegularExpressions.Match class. In the above example, you should use the IsMatch Method.
Related
Update: The acceptable format is ADD|| .
I need to check if the request that the server gets, is in this format, and the numbers are between <>.
After that I have to read the numbers and add them and write the result back. So, if the format not fits to for example ADD|<5>|<8>
I have to refuse it and make a specific error message(it is not a number, it is wrong format, etc.). I checked the ADD| part, I took them in an array, and I can check, if the numbers are not numbers. But I cannot check if the numbers are in <> or not, because the numbers can contain multiple digits and ADD|<7>|<13> is not the same number of items likeADD|<2358>|<78961156>. How can I check that the numbers are in between <>?
please help me with the following: I need to make a server-client console application, and I would like to validate requests from the clients. The acceptable format is XXX|<number>|<number>.
I can split the message like here:
string[] messageProcess = message.Split('|');
and I can check if it is a number or not:
if (!(double.TryParse(messageProcess[1], out double number1)) || !(double.TryParse(messageProcess[2], out double number2)))
but how can I check the <number> part?
Thank you for your advice.
You can use Regex for that.
If I understood you correctly, follwing inputs should pass validation:
xxx|1232|32133
xxx|5345|23423
XXX|1323|45645
and following shouldn't:
YYY|1231|34423
XXX|ds12|sda43
If my assumptions are correct, this Regex should do the trick:
XXX\|\d+\|\d+
What it does?
first it looks for three X's... (if it doesn't matter if it's uppercase or lowercase X substitute XXX with (?:XXX|xxx) or use "case insensitive regex flag" - demo)
separated by pipe (|)...
then looks for more than one digit...
separated by pipe (|)...
finally ending with another set of one or more digits
You can see the demo here: Regex101 Demo
And since you are using C#, the Regex.IsMatch() would probably fit you best. You can read about it here, if you are unfamiliar with regular expressions and how to use them in C#.
Ok, I need a general regular expression that will give me the x characters from a string starting at position y like the string's substring function:
input_str.Substring(y,x)
But as a C# regular expression.
Example:
1234567890 Substring(5,3) 678
I know you are thinking why not just use the Substring function? The short answer is because this goes as a data for an existing function and in this context it would be inelegant to create a whole separate data parsing mechanism. We'd like to get this working without changing the code.
I feel like this is really obvious--but I'm pretty inexperienced with regular expressions. Thanks in advance for any help.
.{y}(.{x}).* should do it, I think, then just pull out the capture group.
I have a seneario where i have to check a word contains "and,or,not,and not" but the regex which i have created fails. Can any body provide me the correct regex for this?
The regex which i have created is like this
Regex objAlphaPattern = new Regex(#"^[not|and not|and|not]");
if(objAlphaPattern.IsMatch(searchTerm))
{
//// code
}
But it always returns true.
I have tried the word "Pen and Pencil" and "Pen Pencil" but both returning true.. Can anybody help in providing correct regex?
You're starting with a begin anchor. If you don't want to only check if it happens at the beginning of the string then you shouldn't have the ^.
Also, you are using [] when you should be using (). Actually in this case you don't even need ().
[] indicates a character class. You just don't need that.
Regex objAlphaPattern = new Regex("\b(and|not)\b");
if(objAlphaPattern.IsMatch(searchTerm))
{
//// code
}
That should do the job.
I highly recommend The Regex Coach to help you build regex.
I also highly recommend http://www.regular-expressions.info/ as a reference.
EDIT:
I feel I should point out you don't really even need the object instance.
if(System.Text.RegularExpressions.Regex.IsMatch(searchTerm, "\b(and|not)\b"))
{
//// code
}
You can just use the static method.
That's a very good point Tim:
"\band\b|\bnot\b"
Another very good point stema:
"\b(and|not)\b"
try
(not)|(and not)|(and)
instead
Your regular expression is wrong, it should be (and|not). There is no need to check for and not either, since it will fail at the first and.
You can use an online tool to check your regular expressions; such as http://regexpal.com/?flags=®ex=(and|not)&input=Pen%20and%20Pencil
Before I look into doing this myself, is there already a function out there that will do this:
I want to pass in a string containing text and RegEx markup and then it returns all possible matches it would look for in a string.
so the following passed to the method
abc|def|xyz
Would return 3 strings in an array or collection:
abc
def
xyz
Because that regex notation says to look for either abc, def or xyz.
I don't want this to search for the term in another string or anything like that, just return the possible matches it could make.
That's a simple example, anything that will do this for me, or shall I start writing the method myself?
With a simple regex as per your example it will work, but as soon as you start dealing with wild cards and repetition, it will have to generate almost an infinite amount of possible solutions, which in some cases may never even terminate.
No, it does not :)
The theme is i opened a file and get all it's data into string and i am matching this string with the regex returning none. But the same regex in PHP is returning values for the same text using preg_match_all. Anyone having a idea?
The method in .NET that’s closest to preg_match_all() is the static Regex.Matches(String,String) call, or the equivalent Matches method on a compiled regular expression. It returns a MatchCollection that you can use to count the matches and to loop over each one.
Can you provide some short, self-contained code to show what’s not working?
There is a Regex.Matches method in C# that you can use.