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
Loading

Kirtan PatelRank #292 · 8.6K reputationPosted Feb 15, 2010, 5:35 AM EST
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 :)
Amit ChoudharyRank #145 · 15.8K reputationPosted Feb 14, 2010, 11:15 PM EST
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.
kurt jackson17 reputationPosted Feb 14, 2010, 11:45 PM EST
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();
Amit ChoudharyRank #145 · 15.8K reputationPosted Feb 15, 2010, 12:23 AM EST
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 ChoudharyRank #145 · 15.8K reputationPosted Feb 15, 2010, 12:41 AM EST
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");
}
kurt jackson17 reputationPosted Feb 15, 2010, 12:45 AM EST
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
Amit ChoudharyRank #145 · 15.8K reputationPosted Feb 15, 2010, 12:50 AM EST
kurt jackson17 reputationPosted Feb 15, 2010, 3:51 PM EST
regards,
kurt
kurt jackson17 reputationPosted Feb 15, 2010, 4:10 PM EST
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
kurt jackson17 reputationPosted Feb 15, 2010, 5:13 PM EST
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