Regular Expression needed

I have a string that is always 9 characters long. I want to remove any 0 that occurs in the middle of the string but NOT remove trailing zero's. 3 examples below.

Original string        desired result
690091500           6991500
690001500           691500
690454600           69454600
691456999           691456999

Can someone help?
boukakaAsked:
Who is Participating?
 
nedfineConnect With a Mentor Commented:
if you want to know how to use regex in C# take a look at this :

http://www.c-sharpcorner.com/UploadFile/prasad_1/RegExpPSD12062005021717AM/RegExpPSD.aspx 


Thanks
Ned
0
 
ozoCommented:
0+(?=\d)
0
 
boukakaAuthor Commented:
Please excuse my ignorance .

If I have string Policy = line.Substring(25,9).. how do I incorporate that regular expression to make it work for me?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
boukakaAuthor Commented:
Thank you, that site is very helpful, I'd like to give you the points because this will solve a lot of my Regex issues going forward. We ended up going a different route with my particular problem. We ended up doing the following which works like a charm.


                        string str;
                        str = "";
                        bool doNothing = false;
                        int nonZero = -1;
                        int firstZero = -1;
                        if(line.Substring(25,9).IndexOf("0") == -1)
                        {
                            doNothing = true;
                        }
                        else
                        {
                            for(int i = 25; i < 35; i++)
                            {
                                if(line.Substring(i,1) == "0")
                                {
                                    firstZero = i;
                                    break;
                                }
                            }
 
                            for(int i = firstZero; i < 35; i++)
                            {
                                if(line.Substring(i,1) != "0")
                                {
                                    nonZero = i;
                                    break;
                                }
                            }
                            if(nonZero == -1)
                            {
                                doNothing = true;
                            }
                        }
                        if(doNothing)
                        {
                            str = line.Substring(25,9);
                        }
                        else
                        {
                            str = line.Substring(25 , firstZero - 25) + line.Substring(nonZero , 34 - nonZero);
                        }

Open in new window

0
 
ddrudikCommented:
You might want to define what you want done with any preceding 0's, if you want to leave any as-is I would try something like this:
Raw Match Pattern:
([1-9]+)(?:0+)?([1-9]+)
 
Raw Replace Pattern:
$1$2
 
C#.NET Code Example:
using System;
using System.Text.RegularExpressions;
namespace myapp
{
  class Class1
    {
      static void Main(string[] args)
        {
          String sourcestring = "source string to match with pattern";
          String matchpattern = @"([1-9]+)(?:0+)?([1-9]+)";
          String replacementpattern = @"$1$2";
          Console.WriteLine(Regex.Replace(sourcestring,matchpattern,replacementpattern));
        }
    }
}
 
$sourcestring after replacement:
6991500

Open in new window

0
 
nedfineCommented:
It is always good to use your own solutions if it works :) ..  
best of luck in your future encounters with regex   :-)


Thanks
Ned

0
 
boukakaAuthor Commented:
Good point! thanks
0
All Courses

From novice to tech pro — start learning today.