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]", "*");
}
Related
This question already has answers here:
Regex for numbers after a certain string part
(2 answers)
Closed 4 years ago.
I want to extract a character after a particular string, example:
Sentence: "Develop1 Tester2 BA3"
String: Develop
Expected result: "1"
I tried the Regex as following but still not get result as my expectation, please consult me, thank in advance.
RegEx: /[DEVELOP\d]\[+-]?\d+(?:\.\d*)?/
You don't really need a regex for this.
Something like this should do the trick:
var input = "Develop1 Tester2 BA3";
var search = "Develop";
if (input.StartsWith(search) && input.Length > search.Length) {
var result = input[search.Length];
Console.Write("Result: " + result);
}
This question already has answers here:
How can I split a string with a string delimiter? [duplicate]
(7 answers)
Closed 4 years ago.
How to find the second part of the string using substring in c#
I have follwwing string:
string str = "George Micahel R - 412352434";
I wanted the above string as two parts.Before "-" and after "-"
I have my first part using following code:
string firstPart = str.Substring(0,str.IndexOf(" - "));
When I am trying for the second part using the following code it gives me an error.
string secondPart = str.Substring(str.IndexOf(" - "), Len(str);
How to get the second part(only number)?
Thanks in advance.
Simply use split function:
var splittedArray = str.Split('-');
var leftPart = splittedArray[0];
var rightPart = splittedArray[1];
Use the Split() method instead.
string str = "George Micahel R - 412352434";
string[] words = str.Split('-');
Console.WriteLine(words[0].Trim()); # George Micahel R
Console.WriteLine(words[1].Trim()); # 412352434
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 |
This question already has answers here:
How do I split a string by a multi-character delimiter in C#?
(10 answers)
Closed 7 years ago.
I have thousands of lines of string type data, and what I need is to extract the string after AS. For example this line:
CASE END AS NoHearing,
What I want would be NoHearing,
This line:
CASE 19083812 END AS NoRequset
What I need would be NoRequset
So far I have tried couple ways of doing it but no success. Can't use .split because AS is not Char type.
If this will be the only way that AS appears in the string:
noRequestString = myString.Substring(myString.IndexOf("AS") + 3);
Using Regex I extract all between the AS and a comma:
string data = #"
CASE END AS NoHearing,
CASE 19083812 END AS NoRequset
";
var items = Regex.Matches(data, #"(?:AS\s+)(?<AsWhat>[^\s,]+)")
.OfType<Match>()
.Select (mt => mt.Groups["AsWhat"])
.ToList();
Console.WriteLine (string.Join(" ", items)); // NoHearing NoRequset
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()) + " ");