Erase all numbers after a underscore - c#

I have this code which should erase all the numbers after a certain _
var fileNameOnly1 = Regex.Replace(fileNameOnly, #"[_\d]", string.Empty);
I.e.
Input
4a_32
abcdef43252_43242
Current Output
4a2
abcdef432523242
Expected output
4a
abcdef43252
I also tried using #"[_\d]"
is there any way to erase numbers after _ and erase the '_' also ??

You dont specifically mention that you need to use regex and in most cases I would advise against it as regex is rather slow (comparison to other methods) and cumbersome (difficult to read and write).
I would think that it would be better to do this using string manipulation instead.
var fileNameOnly1 = fileNameOnly.Split('_')[0];
The above code will find the first '_' and take all characters before it (returned as a string).

Try this
Pattern
_\d+
Example
var fileNameOnly = "asdads_234asd";
var result = Regex.Replace(fileNameOnly, #"_\d+", string.Empty);
Console.WriteLine(result);
Output
asdadsasd

Simply use this regex:
_\d+
Regex.Replace(fileNameOnly, #"_\d+", string.Empty);

Related

C# How do i get the Right(String) based on one character?

I have this:
MyString = #"C:\\Somepath\otherpath\etc\string";
And i need this string (which can be longer than a group of characters)
How can i do something like:
NewString = MyString.Right(string, when last "\" is found) ?
For a path specifically, you can use Path.GetFileName(String).
var MyString = #"C:\Somepath\otherpath\etc\string";
var NewString = Path.GetFileName(MyString);
Despite the name of the method, it also works on directory names, provided they aren't followed by a trailing backslash. So C:\directory becomes directory, but C:\directory\ becomes the empty string. (This might be what you want, based on how you phrased the question.)
Depending on your environment, you might be able to use the new indices and range features that came with C# 8.0
var result = MyString.Split('\\')[^1];
Indices and Ranges
This will return everything after the last instance of the character '\'.
var result = MyString.Substring(MyString.LastIndexOf('\\') + 1);
If you don't mind using a bit of LINQ:
var result = MyString?.Split('\\').LastOrDefault();

Get the first word from the string

I would like to get only the first word of the string regardless of any character or punctuation in front of it.
Sometimes, there could be , or . or !. I don't want these characters.
var s = "Hello, World";
var firstWord = s.Substring(0, s.IndexOf(" "));
This gives me Hello,. I would like to get Hello only.
How do I achieve this?
Simply use the following regex:
var s = "Hello, World";
var result = Regex.Match(s, #"^([\w\-]+)");
Console.WriteLine(result.Value); // Result is "Hello"
This will get the first word regardless of whether or not it ends with punctuation or simply precedes a space.
This will work for you. I assumed that words will be separated with whitespace.
var input = "Hello, World";
var output = Regex.Replace(input.Split()[0], #"[^0-9a-zA-Z\ ]+", "");
IndexOfAny (https://msdn.microsoft.com/fr-ca/library/11w09h50(v=vs.110).aspx) is an alternative if you know the list of characters you want to use. It really depends on the definition you want to use and which characters you want to handle. How do you want to handle characters like œ,é,µ,½,¶,ç,+,-,3...?
Also, do you want to handle locale as some characters might have a classification that is dependant on the language.
Char has many function that allows you to classify characters. See https://msdn.microsoft.com/en-us/library/system.char(v=vs.110).aspx.
And there is also the regex solutions proposed by others.
So the best solution really depends on your need. Do you need to properly handle any Unicode characters or only some specific ASCII characters?
LATE ENTRY:
If you don't want to use Regular Expressions:
private string GetFirstWord(string text)
{
var candidate = text.Trim();
if (!candidate.Any(Char.IsWhiteSpace))
return text;
return candidate.Split(' ').FirstOrDefault();
}

Exclude first and last quotation of string in regex result

I'm running a little c# program where I need to extract the escape-quoted words from a string.
Sample code from linqpad:
string s = "action = 0;\r\ndir = \"C:\\\\folder\\\\\";\r\nresult";
var pattern = "\".*?\"";
var result = Regex.Split(s, pattern);
result.Dump();
Input (actual input contains many more escaped even-number-of quotes):
"action = 0;\r\ndir = \"C:\\\\folder\\\\\";\r\nresult"
expected result
"C:\\folder\\"
actual result (2 items)
"action = 0;
dir = "
_____
";
result"
I get exactly the opposite of what I require. How can I make the regex ignore the starting (and ending) quote of the actual string? Why does it include them in the search? I've used the regex from similar SO questions but still don't get the intended result. I only want to filter by escape quotes.
Instead of using Regex.Split, try Regex.Match.
You don't need RegEx. Simply use String.Split(';') and the second array element will have the path you need. You can then Trim() it to get rid of the quotes and Remove() to get rid of the ndir part. Something like:
result = s.Split(';')[1].Trim("\r ".ToCharArray()).Remove(0, 7).Trim('"');

I need to get a string between two strings using regex in C#

I have a string for example: "GamerTag":"A Talented Boy","GamerTileUrl" and what I have been trying and failing to get is the value: A Talented Boy. I need help creating a regex string to get specifically just A Talented Boy. Can somebody please help me!
var str = "\"GamerTag\":\"A Talented Boy\",\"GamerTileUrl\"";
var colonParts = str.Split(':');
if (colonParts.Length >= 2) {
var commaParts = colonParts[1].Split(',');
var aTalentedBoy = commaParts[0];
var gamerTileUrl = commaParts[1];
}
This allows you to also get other parts of the comma-separated list.
Suppose s is your string (no check here):
s = s.Split(':')[1].Split(',')[0].Trim('"');
If you want to have a Regex solution, here it is:
s = "\"GamerTag\":\"A Talented Boy\",\"GamerTileUrl\"";
Regex reg = new Regex("(?<=:\").+?(?=\")");
s = reg.Match(s).Value;
You can use string methods:
string result = text.Split(':').Last().Split(',').First().Trim('"');
The First/Last extension methods prevent exceptions when the separators are missing.
Demo
I think it's safe to assume that your string is actually bigger than what you showed us and it contains multiple key/value pairs? I think this is will do what you are looking for:
str.Split("GamerTag:\"")[1].Split("\"")[1];
The first split targets "GamerTag:" and gets everything after it. The second split gets everything between first and second " that exists in that chunk after "GamerTag:"
How about this?
\:\"([^\"]+)\"
This matches the semicolon and the opening quote, and matches any non-quote characters until the next quote.

.NET regex replace using backreference

I have a fairly long string that contains sub strings with the following format:
project[1]/someword[1]
project[1]/someotherword[1]
There will be about 10 or so instances of this pattern in the string.
What I want to do is to be able to replace the second integer in square brackets with a different one. So the string would look like this for instance:
project[1]/someword[2]
project[1]/someotherword[2]
I''m thinking that regular expressions are what I need here. I came up with the regex:
project\[1\]/.*\[([0-9])\]
Which should capture the group [0-9] so I can replace it with something else. I'm looking at MSDN Regex.Replace() but I'm not seeing how to replace part of a string that is captured with a value of your choosing. Any advice on how to accomplish this would be appreciated. Thanks much.
*Edit: * After working with #Tharwen some I have changed my approach a bit. Here is the new code I am working with:
String yourString = String yourString = #"<element w:xpath=""/project[1]/someword[1]""/> <anothernode></anothernode> <another element w:xpath=""/project[1]/someotherword[1]""/>";
int yourNumber = 2;
string anotherString = string.Empty;
anotherString = Regex.Replace(yourString, #"(?<=project\[1\]/.*\[)\d(?=\]"")", yourNumber.ToString());
Matched groups are replaced using the $1, $2 syntax as follows :-
csharp> Regex.Replace("Meaning of life is 42", #"([^\d]*)(\d+)", "$1($2)");
"Meaning of life is (42)"
If you are new to regular expressions in .NET I recommend http://www.ultrapico.com/Expresso.htm
Also http://www.regular-expressions.info/dotnet.html has some good stuff for quick reference.
I've adapted yours to use a lookbehind and lookahead to only match a digit which is preceded by 'project[1]/xxxxx[' and followed by ']':
(?<=project\[1\]/.*\[)\d(?=\]")
Then, you can use:
String yourString = "project[1]/someword[1]";
int yourNumber = 2;
yourString = Regex.Replace(yourString, #"(?<=project\[1\]/.*\[)\d(?=\]"")", yourNumber.ToString());
I think maybe you were confused because Regex.Replace has lots of overloads which do slightly different things. I've used this one.
If you want to process the value of a captured group before replacing it, you'll have to separate the different parts of the string, make your modifications and put them back together.
string test = "project[1]/someword[1]\nproject[1]/someotherword[1]\n";
string result = string.Empty;
foreach (Match match in Regex.Matches(test, #"(project\[1\]/.*\[)([0-9])(\]\n)"))
{
result += match.Groups[1].Value;
result += (int.Parse(match.Groups[2].Value) + 1).ToString();
result += match.Groups[3].Value;
}
If you just want to replace text verbatim, it's easier: Regex.Replace(test, #"abc(.*)cba", #"cba$1abc").
you can use String.Replace (String, String)
for example
String.Replace ("someword[1]", "someword[2]")

Categories

Resources