This question already has answers here:
How to check if a String contains any letter from a to z? [duplicate]
(6 answers)
Closed 5 years ago.
I am trying to detect whether my string contains the alphabets (a-z & A-Z), and I obtained my answer from this post. But not all string works as expected, take 8+a as an example:
string expression = "8+a";
if (Regex.IsMatch(expression, #"^[a-zA-Z]+$") == true)
true;
else
false;
This returns false which suppose to be true. How do I make this return true. Thanks!
^ Anchors your regex to the start of the string.
$ Anchors to the end of the string.
Remove those and your regex will work.
Also, there is no need to compare a bool value to true because it is done automatically.
Related
This question already has answers here:
Fastest way to check if string contains only digits in C#
(21 answers)
Closed 2 years ago.
I have a pattern and want to empty the textbox and show a message when the input does not match the pattern.
I have tried putting the erasing line in else, and used a negation ! in front of the condition.
if (!Regex.IsMatch(MyTextBox.Text, ".*?[0-9].*?"))
{
MyTextBox.Text = "";
MessageBox.Show("Please input only numbers");
}
But both of these solution result in that if ANY symbol in the textbox matches the pattern gives an error when I want to float.parse the string later on.
Examples:
1234 should and does work
A Stops you as it should
1A Doesn't stop you, but should
Try to use this:
if (!Regex.IsMatch(textbox.Text, "^[0-9]*$")){
....
}
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I'm trying to check if there are 4 symbols in a row in a string
For example
abcde/-*,f€gh
I want to match if there is /-*,
but the symbols can change to any non-letter, non-digit or non-space
I think this oughta just do it:
([^a-zA-Z0-9\s]){4,}
Can use it like so:
Console.WriteLine(Regex.Match("abcde/-*,f€gh", #"([^a-zA-Z0-9\s]){4,}").Success); // Prints True
Console.WriteLine(Regex.Match("abcde/- *,f€gh", #"([^a-zA-Z0-9\s]){4,}").Success); // Prints False
This question already has answers here:
C# Code to generate strings that match a regex [closed]
(4 answers)
Closed 3 years ago.
Based off a regex string I would like to get a list of all the possible strings that would match the regex.
Example:
Given a regex string like...
^(en/|)resources/case(-| )studies/
I want to get a list of all the possible strings that would match the regex expression. Like...
^en/resources/case-studies/
or
^/resources/case-studies/
or
^en/resources/case studies/
or
^/resources/case studies/
Thank you
Note that in regex ^ denotes the beginning of the line. You must escape it
Try
\^(en)?/resources/case(-|\s)studies/
explanation:
\^ is ^ escaped.
(en)? is optionally en, where ? means zero or one times.
/resources/case the text as is.
(-|\s) minus sign or white space.
studies/ the text as is.
See: https://dotnetfiddle.net/PO4wKV
This question already has answers here:
Reference - What does this regex mean?
(1 answer)
What do ^ and $ mean in a regular expression?
(2 answers)
Closed 3 years ago.
I've already read this here and I still have got some questions. I will be very grateful if you can help me to solve them. I'm trying to compose a RegEx to verify that a string contains only letters, numbers, underscores and hyphens. Firstly, when I tried to do it (without Google-search) I did this #"[A-Za-z0-9_-]". After I made some research I found that it should be #"^[a-zA-Z0-9_-]$" where:
^ asserts position at start of a line
$ asserts position at the end of a line
My question is why we should insert these symbols? And my other question is why the string "jeffbutt" (with yellow in the screenshot) doesn't match?
This question already has answers here:
Regular expression to match characters at beginning of line only
(8 answers)
Closed 7 years ago.
I have a string of type "12.43 some non important stuff", I want to get the number 12.43, but only if it is at exact position, which is 0 in this case.
"\d+" will return 12.43, but it will return it even in case of "hello world 12.43 some non important stuff".
Is there an easy way (maybe not regex even) to get the decimal only if it's on desired position?
You can use ^ (start anchor)
^\d+
If you know a specific position, (say 5th) you can do the following:
^.{4}(\d+)