Reputation: 23
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 ??
Upvotes: 1
Views: 53
Reputation: 685
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).
Upvotes: 2
Reputation: 56202
Simply use this regex:
_\d+
Regex.Replace(fileNameOnly, @"_\d+", string.Empty);
Upvotes: 0
Reputation: 81563
Try this
Pattern
_\d+
Example
var fileNameOnly = "asdads_234asd";
var result = Regex.Replace(fileNameOnly, @"_\d+", string.Empty);
Console.WriteLine(result);
Output
asdadsasd
Upvotes: 0