This question already has answers here:
How do I split a string by a multi-character delimiter in C#?
(10 answers)
Closed 4 years ago.
How can I match this string in c# using regex so it returns 4 groups per line?
1 or more digits|one or more letters|one or more letters|one ore more X-Digit(s)\n
Example:
123|ABC|ABC|X-1;X-12;X-13
123|ABC|ABC|X-1
I've tried this
\d+\|(A-Z)\|(A-Z)\|(X-)d+
Why shooting with canons at birds?=! If you could simply use the String.Split method to achieve that
string test = "123|ABC|ABC|X-1;X-12;X-13";
string [] groups = test.Split('|');
it will return an array of elements that were separated by a |
Related
This question already has answers here:
split a string on newlines in .NET
(17 answers)
Closed 1 year ago.
I need to convert string to IEnumerable<string> by every newline character.
I just want the same functionality as File.ReadLines without loading any file, rather taking it from a previously loaded string.
String.Split can split your string and give you string[]
Usage
string someString = ...;
string[] lines = someString.Split('\n');
IEnumerable<string> linesAsEnumerable = lines;
DoSomethingWithLines(linesAsEnumerable);
You could split it according to the newline character:
String[] lines = fileContenets.Split('\n');
This question already has answers here:
mask all digits except first 6 and last 4 digits of a string( length varies )
(13 answers)
Closed 4 years ago.
I'm using C# to create a pattern in order to replace all the occurrences in a string from
RX123456789 into RX*********
I have tried various patterns without success. I'm kinda new regarding Regular Expression.
I appreciate your help.
Use this Regex:-
string data = "RX123456789";
var resultString = Regex.Replace(data, #"[0-9]", "*");
If you need * of number only When RX is present then use this logic:-
string data = "RX123456789";
var resultString="";
if (new Regex("RX([0-9]+)").IsMatch(data))
{
resultString = Regex.Replace(data, #"[0-9]", "*");
}
This question already has answers here:
RegEx Starts with [ and ending with ]
(4 answers)
Closed 6 years ago.
How to replace a set of characters where I only know the first and the last one, in between is a variable that is not constant.
All I know is that this string will always start with & and it will end with ;
string str = "Hello &145126451; mate!";
How to get rid of &145126451; ?
So the desired result is:
string result = "Hello mate!"
The most easiest way is to use Regex:
Regex yourRegex = new Regex(#"&.*;");
string result = yourRegex.Replace("Hello &145126451; mate!", String.Empty);
Console.WriteLine(result);
Here is a fiddle with example.
This question already has an answer here:
Splitting the array with / slash
(1 answer)
Closed 7 years ago.
I have a string which includes integers between slashes.
For example:
string myString = "/1//2//56//21/";
I need to take these integers and add them into a list. How can I split this string to its integers?
string myString = "/1//2//56//21/";
int[] arrayInt = Regex.Split(myString, "/+").Where(s => !String.IsNullOrWhiteSpace(s)).Select(Int32.Parse).ToArray();
This question already has answers here:
Closed 14 years ago.
I would like to take a pascal-cased string like "CountOfWidgets" and convert it into something more user-friendly like "Count of Widgets" in C#. Multiple adjacent uppercase characters should be left intact. What is the most efficient way to do this?
NOTE: Duplicate of .NET - How can you split a "caps" delimited string into an array?
Don't know about efficient but at least it's terse:
Regex r = new Regex("([A-Z]+[a-z]+)");
string result = r.Replace("CountOfWidgets", m => (m.Value.Length > 3 ? m.Value : m.Value.ToLower()) + " ");