none
Regex: Ignoring / skipping digits within an input-string RRS feed

  • Question

  • Hello@all,

    I´m using REGEX to query an input string for different formats. All regular expressions are stored in a table - through looping throough the dataset I do a Regex.Match against each table entry.

    Anyway I got a new request which looks as follows:

    Input string: 0400002078000100

    I´m interested only in the fist 10 Digits (therefor I use: 04000[0-9]{5}), ignore the next two digits ([0-9]{2})  and read again the next two digits.

    Result of the match should be 040000307801 .

    Thank you for any help,

    Best regards,

    Andre

    Monday, July 22, 2013 1:14 PM

Answers

  • Hi Andre,

    I'm not similar with Regex,now I use another way to solve it.I hope it can help you !

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace SplitStirng
    {
        class Program
        {
            static void Main(string[] args)
            {
                string s = "0400002078000100";
                string result = GetResultString(s);
                Console.WriteLine(result);
            }
            private static string GetResultString(string s)
            {
                if (s.Length <= 10)
                {
                    return s;
                }
                string s1 = s.Substring(0, 10);
                string s2 = s.Substring(10);
                StringBuilder sb = new StringBuilder();
                while (s2.Length > 2)
                {
                    if (s2.Length == 3)
                    {
                        sb.Append(s2.Substring(2, 1));
                        break;
                    }
                    else
                    {
                        sb.Append(s2.Substring(2, 2));
                    }
                    s2 = s2.Substring(4);
                }
                return s1 + sb.ToString();
            }
        }
    }

    Regards,

    Damon

    Tuesday, July 23, 2013 6:42 AM
  • string

    input = "0400002078000100";

    Regex exp = new Regex("(04000[0-9]{5})[0-9]{2}([0-9]{2})");

    //expected results "040000307801"

    Match match = exp.Match(input);

    string results = match.Groups[1].Value + match.Groups[2].Value;


    jdweng

    Tuesday, July 23, 2013 7:53 AM
  • String input = "0400002078000100";
    Match match = Regex.Match(input,@"(\d{10})(\d{2})(\d{2})");
    	if (match.Success)
    	{
    	    string s1 = match.Groups[1].Value;
    	    String s2 = match.Groups[3].Value;
    	    Console.WriteLine("{0}{1}",s1,s2);
    	}


    Tuesday, July 23, 2013 8:14 AM

All replies

  • Hi Andre,

    I'm not similar with Regex,now I use another way to solve it.I hope it can help you !

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace SplitStirng
    {
        class Program
        {
            static void Main(string[] args)
            {
                string s = "0400002078000100";
                string result = GetResultString(s);
                Console.WriteLine(result);
            }
            private static string GetResultString(string s)
            {
                if (s.Length <= 10)
                {
                    return s;
                }
                string s1 = s.Substring(0, 10);
                string s2 = s.Substring(10);
                StringBuilder sb = new StringBuilder();
                while (s2.Length > 2)
                {
                    if (s2.Length == 3)
                    {
                        sb.Append(s2.Substring(2, 1));
                        break;
                    }
                    else
                    {
                        sb.Append(s2.Substring(2, 2));
                    }
                    s2 = s2.Substring(4);
                }
                return s1 + sb.ToString();
            }
        }
    }

    Regards,

    Damon

    Tuesday, July 23, 2013 6:42 AM
  • string

    input = "0400002078000100";

    Regex exp = new Regex("(04000[0-9]{5})[0-9]{2}([0-9]{2})");

    //expected results "040000307801"

    Match match = exp.Match(input);

    string results = match.Groups[1].Value + match.Groups[2].Value;


    jdweng

    Tuesday, July 23, 2013 7:53 AM
  • String input = "0400002078000100";
    Match match = Regex.Match(input,@"(\d{10})(\d{2})(\d{2})");
    	if (match.Success)
    	{
    	    string s1 = match.Groups[1].Value;
    	    String s2 = match.Groups[3].Value;
    	    Console.WriteLine("{0}{1}",s1,s2);
    	}


    Tuesday, July 23, 2013 8:14 AM