0

How to remove first numeric characters from a string using regular expressions? string str = 20Be45;

Output -> Be45

Krisztián Balla
  • 19,223
  • 13
  • 68
  • 84
VJOY
  • 3,752
  • 12
  • 57
  • 90

3 Answers3

8

This is the Regex

string text = "20Be45";
string replaced = Regex.Replace(text, "^[0-9]+", string.Empty);

BUT, I programmed for 15 years without using regexes, and you know what? I was happy, and my programs worked.

int i = 0;

while (i < text.Length && text[i] >= '0' && text[i] <= '9')
{
    i++;
}

text = text.Substring(i);
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • char.IsNumber or char.IsDigit? – James Apr 08 '15 at 10:26
  • 1
    @JamesBarrass `char.IsDigit` probably better... But I'm very old-skool, so I still use the >= '0' <= '9' comparison that is very easy to read. (and has the advantage that it is perfectly readable, without needing to think if it means unicode digit or 0-9 digit as with the dichotomy IsDigit/IsNumber) – xanatos Apr 08 '15 at 10:27
  • 3
    [Regexp: Now you have two problems](http://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/) ;) – Matthew Watson Apr 08 '15 at 10:31
  • Too old school for me, and I have about the same number of years programming as you... a regular expression is the better way to deal with things like that. – Zohar Peled Apr 08 '15 at 10:32
  • @ZoharPeled C doesn't have a regexp included... C++ didn't have until some years ago. VB 6 didn't have. Then there is the other "branch" (Perl/PHP/Python) that have. – xanatos Apr 08 '15 at 10:33
  • How is that relevant? the question was about c# and regex. btw, as far as I can remember, javascript supports regex for at least 15 years. – Zohar Peled Apr 08 '15 at 10:41
  • @ZoharPeled That you followed a different road to programming. C/C++ and VB programmers didn't use regexes, while P* programmers (and Javascript) (so Web programmers) did. – xanatos Apr 08 '15 at 10:42
  • While I agree that you don't need to use Regex here, I don't think that you should re-invent the wheel. .NET framework comes with a lots of functionality that is written by smart guys and is ready to be used. Use that instead. – Kaspars Ozols Apr 08 '15 at 10:43
  • @xanatos: Why does that matter? I can't even read php, but I have worked for a long time with javascript. Is there something wrong with that? Do you think that the language someone uses to write code makes that someone a better programmer? – Zohar Peled Apr 08 '15 at 10:49
  • @ZoharPeled I'm saying that until some years ago half of the programmers couldn't use Regexese, but still lived happy. They couldn't use them because their languages didn't support them. The other half of the programmers used languages that support them (and often used them). Some of these languages supported regexes as first level objects (like Javascript). I haven't said anything else. – xanatos Apr 08 '15 at 10:51
  • Ok, so you have some issue against regular expressions. I get that, a lot of programmers don't like regular expression and for good reasons. but for questions like this, where the regular expression is very simple and readable I see no good reason to loop over the string like you did. if he asked to verify that an date is valid, that would be a totally different story. – Zohar Peled Apr 08 '15 at 11:04
4

This is another solution using the String.TrimStart method:

string text = "20Be45";
string replaced = text.TrimStart('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
Krisztián Balla
  • 19,223
  • 13
  • 68
  • 84
1

Another non-Regex version:

var text = "20Be45";
var result = string.Concat(text.SkipWhile(char.IsDigit));
Kaspars Ozols
  • 6,967
  • 1
  • 20
  • 33