This question already has answers here:
How to split string with Regex.Split and keep all separators?
(2 answers)
Closed 2 years ago.
I was wondering how to Split a string in C# but keeping the delimiters at the start of each substring.
Example:
Input: "1,2,3,4,5,6"
Delimiter: ','
Output: {'1', ',2', ',3', ',4', ',5', ',6'}
At the moment I only know to keep the delimiter at the end of each substring using Regex.Split from this answer.
String text = "1,2,3,4,5,6";
var split = Regex.Split(text, #"(?<=,)");
Instead of lookbehind you need to use a lookahead for splitting:
(?=,)
RegEx Demo
What you want is splitting on a position when you have comma at next position that makes it a lookahead assertion. On the other hand a lookbehind assertion will split when we have comma at previous position thus splitting after comma not before it.
Code:
String text = "1,2,3,4,5,6";
var split = Regex.Split(text, #"(?=,)");
//=> ["1", ",2", ",3", ",4", ",5", ",6"]
Related
This question already has answers here:
Regular expression to validate numbers separated by commas or dashes [closed]
(2 answers)
Closed 2 years ago.
I have to write the Regex expression which accepts - and only numbers either single four digit number or two 4 digit numbers seperated by hyphen as shown below
2751, 2759-2764, 2766-2774, 2776-2777, 2890-2897
3945-3974, 3979, 3984-3999
I have used this Regex ^[0-9_,]+ but this line Regex.IsMatch(line, #"^[0-9_,]+$") returns false.
Regards,
Nagasree
The pattern that you tried is not matching as there is no hyphen or space in the character class. But when you would add those, the pattern still does not take any format into account.
You could match 4 digits with optional hyphen and 4 digits part. Then repeat that preceded by a space:
^[0-9]{4}(?:-[0-9]{4})?(?:, [0-9]{4}(?:-[0-9]{4})?)*$
Regex demo
var s = "2751, 2759-2764, 2766-2774, 2776-2777, 2890-2897";
Console.WriteLine(Regex.IsMatch(s, #"^[0-9]{4}(?:-[0-9]{4})?(?:, [0-9]{4}(?:-[0-9]{4})?)*$"));
Output
True
This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
How to make a regex match case insensitive?
(1 answer)
Closed 5 years ago.
I am trying to do a find replace in a string of text. I am using Regex like this:
Regex regexText = new Regex("Test.Value");
strText = regexText.Replace(strText, value);
In this example I am trying to find the string "Test.Value" in a text string. However if this value appears in the string the replace does not happen.
If I remove the dots eg:
Regex regexText = new Regex("TEST");
strText = regexText.Replace(strText, value);
If I put the word "TEST" in the string, it replaces it just fine.
Is there a way to get this to work with strings with "."'s in?
You have to escape the dot:
Regex regexText = new Regex(#"Test\.Value");
As you wrote it, the regex is just looking for "Test", followed by any character except a line feed, followed by "Value".
On the top of that, if the text you are looking for is a little bit different, a case insensitive matching could help you out:
Regex regexText = new Regex(#"Test\.Value", RegexOptions.IgnoreCase);
Anyway, in this case I don't think a Regex is necessary. A simple string replace should do the job:
strText.Replace("Test.Value", value);
This question already has answers here:
.NET RegEx for letters and spaces
(5 answers)
Closed 4 years ago.
I need to save in a string only numbers and letters and white spaces. I need to use System.Text.RegularExpressions.Regex. If I put the string in a IF clause, it will only be valid if it contains only numbers, letters and white spaces
This should do the trick
if (Regex.IsMatch(yourstring, #"^[\d \w \s]+$"))
{
Console.WriteLine("passed");
}
Explanation:
^: designates the beginning of a string
$: designates the end of a string
[...] matches all caracters inside the parantheses
\d matches digits
\w matches letters
\s matches spaces
+ means 1 or more occurences
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:
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"