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(',', '.');
Related
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:
Regex to pick characters outside of pair of quotes
(6 answers)
Closed 7 years ago.
I am new in writing regular expression and I have the following Scenario.
I have a string, like :
string line = "if (true){var data = string.Format(\"something {0} {1}.\", \"is\", \"wrong\");}";
now I need to write a regular expression that just pick the closing curly braces which are not in the double quote
so far I tried this:
"(^(\"[^\"]*\")(}))+"
^(\"[^\"]*\") : I want to Ignore any substring which is inside double quote, AND
(}) : I want to take }
+: for at least 1 occurrence.
But it seems I Did something wrong. Could any one please guide me to sort out where I did the wrong?
Thank you.
You just need these parts of your regex:
(?:\"[^\"]*\")|(})
Regex live here.
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/
This question already has answers here:
C# string splitting
(4 answers)
Closed 7 years ago.
I need to be able to extract the three strings separated by a dashes "-" from an input string.
For example:
Mystring="54-0-9";
the values separated by "-" are with unknown length because they should be an input for the user.
The user should enter each value in a textField than my code concatenate the 3 values and put it as shown . Later i want to get the three values separated again each one in a new text field. How can i do that in c# !?
Using string split.
// Select on "-".
string[] split = _string.Split(new Char[] { '-' });
split[0], split[1], split[2] will have the values you want.
Mystring="54-0-9";
Mystring.Split('-');
this will give you a array of 3 now.
This question already has answers here:
Fastest way to remove the leading special characters in string in c#
(2 answers)
Closed 8 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I want to trim leading whitespace and the single quote using one call to Trim without calling it twice as follows.
string s = " 'hello'";
var newString = s.Trim().Trim('\'');
I don't want to use
var newString = s.TrimStart().Trim(''\').
either as it is two calls.
Use the overload of Trim that accepts multiple characters:
string s = " 'hello'";
var newString = s.Trim(' ', '\'');
Although there are several caveats:
your question only mentions leading whitespace, but Trim removes trailing characters as well. If you only want leading characters use TrimStart instead.
this solution only removes full spaces, not all whitespace. Technically you would have to add all characters that are considered "whitespace". If you need to trim more than just spaces, then calling Trim twice will be cleaner.
This solution would also Trim whitespace within the apostrophes:
string s = " ' hello'";
var newString = s.Trim(' ', '\''); // returns "hello"