Employers - Post Free Jobs
Forum guidelines
AuthorQuestion
RegEx Problem..
Posted on: 15 Feb 2010

Hi folks,
Good day! I need some help on regex pattern to achieve this output: 123/1234567
Example:
Input: 1234567
Expected  Output: 123/1234567
Rule: 1. Get first the first 3 digit {123}
          2. Append / symbol {/}
         3. Concat the original Input {1234567}
         4. Expected output {123/1234567}
 
I have this patter in my code:
string input = "1234567";
string output = "";
regex rx = new regex(@"([\w]{1,3})([\w]{1,7})")
Match
match = null;
match = rx.match(input)
if (match.Groups.Count > 1)
   for (int i = 1; i < match.Groups.Count; i++)
      output +=
"/" + match.Groups[i].ToString();
 
but my output value is looks like this: 123/4567     which is wrong cause the expected output should looks like this: 123/1234567
I hope someone could help me in this problem. Thank you so much!
regards,
kurt
 

try and try until you success.
AuthorReply
Kirtan Patel
  • 0
  • 0
accepted
Re: RegEx Problem..
Posted on: 15 Feb 2010   Accepted Answer

            Hi Here is your Solution,


            string str = "1234567";

            Regex rex = new Regex("^[0-9]{3}");

            Match m = rex.Match(str);

            string FinalString = m.Value.ToString() + "/" + str;

            MessageBox.Show(FinalString);


Even you can do this without Regular Expressions too m using simple codes ..like substr() function


if any problem arise let me know :)


MY BLOG ( www.codestructs.com ) | MY SITE

Kirtan
 Patel   BCA   MCA (SEMESTER-IV)  MINDCRACKER MVP WEB HACKER



Re: RegEx Problem..
Posted on: 15 Feb 2010  
hi friend,
According to your rule

Rule: 1. Get first the first 3 digit {123}
          2. Append / symbol {/}
         3. Concat the original Input {1234567}
         4. Expected output {123/1234567}


your regex will be  "[\d]{3}/[\d]*" to match the "123/1234567"

I hope it helps you. so don't forget to mark it as answer.

=====================================
Amit Choudhary
MicrosoftMVP MindcrackerMVP Blog: www.cshandler.com
Follow @vendettamit

Re: RegEx Problem..
Posted on: 15 Feb 2010  

hi amit , thanks for your quick reply however, it's not work and i got this error : " Object reference not set to an instance of an object." 
here's the code:
string input = "1234567";
string output = "";
regex rx = new regex(@"[\d]{3}/[\d]")
Match
match = null;
match = rx.match(input)
if (match.Groups.Count > 1)
   for (int i = 1; i < match.Groups.Count; i++)
      output +=
"/" + match.Groups[i].ToString();

try and try until you success.
Re: RegEx Problem..
Posted on: 15 Feb 2010  
hi friend,

you are missing some thing in your regex statement:

regex rx = new regex(@"[\d]{3}/[\d]");

regex rx = new regex(@"[\d]{3}/[\d]*")

the * is missing in the above regular expression.

Try it out if it works.

Otherwise send me your complete requirement i'll try to write some code for you.

=====
Dont forget to mark the answer if it helps




=====================================
Amit Choudhary
MicrosoftMVP MindcrackerMVP Blog: www.cshandler.com
Follow @vendettamit

Re: RegEx Problem..
Posted on: 15 Feb 2010  
I hope your requirement may be fulfilled by the following code:
  
    string input = "1234567";
        string output = "";
        for (int i = 0; i < 3; i++)
        {
            output += input[i];
        }
        output += "/"+input;

        Regex reg = new Regex(@"[\d]{3}/[\d]*");
        if (reg.Equals(output))
        {
            Console.WriteLine("Output is Formatted");
        }

=====================================
Amit Choudhary
MicrosoftMVP MindcrackerMVP Blog: www.cshandler.com
Follow @vendettamit

Re: RegEx Problem..
Posted on: 15 Feb 2010  

hello amit, thank you so much.  but unfortunately, it's still not working. Here's my complete code.
 
Regex re = null;
Match match = null;
string input = "1234567";
string output = null;
re =
new Regex(@"([\d]{3}/[\d]*)");
match = re.Match(input);
if (match.Groups.Count > 1)
for (int i = 1; i < match.Groups.Count; i++)
output += match.Groups[i].ToString();

please kindly copy and paste my code so that you would also configured out the error. Thank you friend for being helpful.
 
regards,
kurt
 

try and try until you success.
Re: RegEx Problem..
Posted on: 15 Feb 2010  
Try the code i sent you in above post.

=====================================
Amit Choudhary
MicrosoftMVP MindcrackerMVP Blog: www.cshandler.com
Follow @vendettamit

Re: RegEx Problem..
Posted on: 15 Feb 2010  

hi Amit, thanks again. But friend I want to achieve the output using the RegEx Pattern without using data manipulation. I already get the exact output using substring() and the reason why I force to use the RegEX Pattern because this pattern will set to my config file so that there will be no data manipulation inside my code at all. So if ever in the future if the  output would be change , it is easy for me to change the code in the config file.  thank you friend.
 
regards,
kurt

try and try until you success.
Re: RegEx Problem..
Posted on: 15 Feb 2010  

Hi Kirtan ,

Good day!
I have a little problem on RegEx pattern in c#. Please take note it should be done using RegEx pattern only. I already resolved this using substring()
but I need to convert it using RegEx because I'll put the RegEx pattern on my config file so that there will be no data manipulatin inside my code.
Here's the rule below:
input: 1234567
expected output: 123/1234567
Rule: 1. Get the first three digit in the input. //123
      2. Add /
      3. Append the the original input. //1234567
      4. The expected output should looks like this: 123/1234567
here's my regex pattern:
regex rx = new regex(@"((\w{1,3})(\w{1,7}))");
but the output is incorrect. 123/4567
Any helps from you guys?
Thank you!
regards,
kurt

try and try until you success.
Re: RegEx Problem..
Posted on: 15 Feb 2010  

Hi Amit and Kitan,
You guys are very helpful and I thank you guys for the quick reply on my problem. Thank you so much.
I already figured out the solution on my problem. Instead using RegEx.Match I used RegEx.Replace.
Here's my edited code;
Input: 1234567
output:
output=
Regex.Replace(Input, @"(\w{3})(\w+)", @"$1/$1$2");
Console.WriteLine("Output: " + output.ToString());
Thanks again guys and god bless!
regards,
kurt

try and try until you success.

SPONSORED BY

Offshore Software and Database Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.