Regex: Stripping Unwanted Chararcters

by ssi 10. June 2016 07:51
public static string StripLeadingZeros(string sAlphaNumberText)
       {
           /*
            * That will trim trailing zeros if they appear after the period in a decimal number (it will always leave a single zero).
            *  It also accounts for numbers that appear at the very end.
            */
           string sRet = "";
 
           sRet = Regex.Replace(sAlphaNumberText,
                        @"(?<=\.\d+?)0+(?=\D|$)",
                        String.Empty);
           return sRet;
       }
 
       public static string StripWhitespace(string sAlphaNumberText)
       {
           /*
            * sStrip Whitespace
            */
           string sRet = "";
 
           sRet = Regex.Replace(sAlphaNumberText, @"\s+""");
           return sRet;
       }
       public static string StripAllButNumbers(string sAlphaNumberText)
       {
           /*
            * strip all characters except numbers
            */
           string sRet = "";
 
           sRet = Regex.Replace(sAlphaNumberText, @"[^\d]"String.Empty);
           return sRet;
       }
       public static string StripNumbers(string sAlphaNumberText)
       {
           /*
            * sStrip Numbers
            */
           string sRet = "";
 
           sRet = Regex.Replace(sAlphaNumberText, @"[\d-]"string.Empty);
           return sRet;
       }
 
       public static string StripAllSpecialCharacters(string sAlphaNumberText)
       {
           /*
            * Strip All Special Characters
            */
           string sRet = "";
 
           sRet = Regex.Replace(sAlphaNumberText, "[^0-9a-zA-Z]+""");
           return sRet;
       }

Tags: , , ,

Blog | CSharp | Regex

Ge tCity State Zip REGEX

by ssi 17. December 2015 17:43

   private string[] GetCityStateZip(string sAddress)

        {

            //string[] split = sAddress.Split(new Char[] { ' ', });

            //return split;

            sAddress = sAddress.Replace(".", "");

            sAddress = sAddress.Replace(",", ", ");

            sAddress = sAddress.Replace("  ", " ");

            string[] aRet = new string[3];

            Regex addressPattern = new Regex(@"(?<city>[A-Za-z',.\s]+) (?<state>([A-Za-z]{2}|[A-Za-z]{2},))\s*(?<zip>\d{5}(-\d{4})|\d{5})");

 

            MatchCollection matches = addressPattern.Matches(sAddress);

 

            for (int mc = 0; mc < matches.Count; mc++)

            {

                aRet[pADDR_CITY] = matches[mc].Groups["city"].Value;

                aRet[pADDR_STATE] = matches[mc].Groups["state"].Value;

                aRet[pADDR_ZIP] = matches[mc].Groups["zip"].Value;

            }

            return aRet;

        }

 

Tags:

CSharp | Regex

C # Regex Extract City, State zip from Address string

by ssi 1. May 2013 17:40
Useage:   string[] CityStateZip = GetCityStateZip(cus.ADD3);
 

private string[] GetCityStateZip(string sAddress)         {             //string[] split = sAddress.Split(new Char[] { ' ', });             //return split;             sAddress = sAddress.Replace(".""");             sAddress = sAddress.Replace(","", ");             sAddress = sAddress.Replace("  "" ");             string[] aRet = new string[3];             Regex addressPattern = new Regex(@"(?<city>[A-Za-z',.\s]+) (?<state>([A-Za-z]{2}|[A-Za-z]{2},))\s*(?<zip>\d{5}(-\d{4})|\d{5})");             MatchCollection matches = addressPattern.Matches(sAddress);             for (int mc = 0; mc < matches.Count; mc++)             {                 aRet[pADDR_CITY] = matches[mc].Groups["city"].Value;                 aRet[pADDR_STATE] = matches[mc].Groups["state"].Value;                  aRet[pADDR_ZIP] = matches[mc].Groups["zip"].Value;             }             return aRet;         }

Tags: , ,

Regex

Calendar

<<  December 2025  >>
MoTuWeThFrSaSu
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

RecentComments

None

Development Team @ Shelbysys

We develop custom database applications for our clients. Our development tool of choice is MS Visual Studio. 

Quotations

"Fill the unforgiving minute with sixty seconds worth of distance run."
Rudyard Kipling