This question already has answers here:
Regex to get NUMBER only from String
(3 answers)
Closed 7 years ago.
Im taking a selected.text which can equal 123t, and id like to just take the 123 and insert it into a string. the selected.text can also simply equal 54, or 256, but occasionally it will have a letter. Im unsure of which what to select or parse to remove the letter. i tried to do something convoluted like this as i saw similar questions on SE,
string cat;
int dog;
cat = txtFrame.Text;
dog = int.Parse(cat.Substring(cat.IndexOf("")));
Frame = dog.ToString();
Frame is the string i would like it to end up in.
This is probably best done with Regex
string test = "123t";
Match m = Regex.Match(test, #"\d+");
Console.WriteLine(m.Value);
If an eventual sign is possible in your string then you could use an expanded form of the pattern
Regex.Match(test, #"[-+]?\d+")
From the following post ...
How do you remove all the alphabetic characters from a string?
Regex.Replace(s, "[^0-9.+-]", "")
#Steve's answer is better!
Here's a good resource for RegEx ... http://www.regular-expressions.info/
Related
This question already has answers here:
Can I use variables in pattern in Regex (C#)
(2 answers)
Closed 3 years ago.
Is it possible to place a string variable inside a Regex? If so.. how?
I've been playing with regex for 4 hours now and i need just one more thing to finish.
return (new Regex(#"\bA=(\d+[/]\d+)").Match(From).Groups[1].Value.Trim()).ToString();
This line basically gets any fractional number like 42/13 only if it's after "A=" from a string and extracts it.
So here's my question - Is it possible to do something like that:
string variable;
Regex(#"\b"variable"=(\d+[/]\d+)").Match(From).Groups[1].Value.Trim()).ToString();
The idea is to make it so whatever is in variable becomes the regex and for example if in the variable we input D it's now D= now A=.
Thanks in advance.
This is string interpolation. You use the $ operator on your strings to use it. Example
string variable = "hello";
Regex regex = new Regex($#"\b{Regex.Escape(variable)}=(\d+[/]\d+)");
You need to concatenate your strings as usual (+) and prepend # to each string if using backslashes without escaping them. You also don't need to encase / in the character class as [/]. Alternatively, as mentioned by Josh in his answer and Ron Beyer in his comment below your question, you can use interpolation.
#"\b" + variable + #"=(\d+/\d+)"
Additionally, you should use the method Regex.Escape() against your variable to ensure any special characters are escaped (this will prevent your pattern from failing or making incorrect matches) - sanitizing your variable.
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 answers here:
How to remove the last comma and space from string (if there) using regex?
(4 answers)
Closed 7 years ago.
I have a string that can end will several "X" characters.
Let's say the string ends in commas like this.
string X = "1,2,3,,,";
I need some function or lambda expression that can remove the last set of commas.
I will have no way of knowing how many commas there are at the end of the string but the end result should look like this.
?x -- "1,2,3";
You dont need regex or LINQ, use String.TrimEnd:
X = X.TrimEnd(',');
You can use it with multiple characters, f.e. if you want to remove trailing commas and dots:
X = X.TrimEnd(',', '.');
This question already has answers here:
How to match emoticons with regular expressions?
(3 answers)
Closed 9 years ago.
If I have string such as "I love my country :) :D. I like myself :P -_- .", how to remove everything except Emoticons - so the resulting string should be without any text ?
Input String or text can be any type.
I am using Regex
Regex.Replace(str, "[A-Za-z]", "");
but it also remove "P""D" in ":D :P" smiley. What will be the Regex then?
Thanks in advance.
There is a lot of emoticons so so you wil. end with a very long and over complicated regular expression but. In this case I think you only care of Two 'corrupted' emoticons after the replace. So if this is the case, this should work:
[ABCE-OQ-Za-oq-z]|(?<!:)D|(?<!:)[Pp]
This regular expresssion match on ABC, the range from E to O and then the ragne of Q to Z for lowercase letters it matches from a to o and from q to z. The key part in the regular expression is that it only matches D, P and p if the matched char is not preceded by a colon. This feature is called lookaround (or in this exact use case lookbehind).
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 3 years ago.
Let's say I have a multi-line string like this:
STARTFRUIT
banana
ENDFRUIT
STARTFRUIT
avocado
ENDFRUIT
STARTVEGGIE
rhubarb
ENDVEGGIE
STARTFRUIT
lime
ENDFRUIT
I want to search for all fruit, no veggies. I try this:
MatchCollection myMatches = Regex.Matches(tbBlob.Text, "STARTFRUIT.*ENDFRUIT", RegexOptions.Singleline);
foreach (var myMatch in myMatches)
{
Forms.MessageBox.Show(String.Format("Match: {0}", myMatch), "Match", Forms.MessageBoxButtons.OK, Forms.MessageBoxIcon.Information);
}
The problem is, instead of returning me an array of three matches, it gives me a big match encompassing the first STARTFRUIT at the beginning and the last ENDFRUIT at the end. Is there a way to "minimalize" the match search? I don't see any help in RegexOptions.
Use a non-greedy modifier (a question mark) after the quantifier:
"STARTFRUIT.*?ENDFRUIT"
^
add this
Note that the question-mark here has a different meaning here than when it is used as a quantifier, where it means "match zero or one".