16

I would like to format a string that looks like this

BPT4SH9R0XJ6

Into something that looks like this

BPT4-SH9R-0XJ6

The string will always be a mix of 12 letters and numbers

Any advice will be highly appreciated, thanks

5
  • 6
    Is there a reason you need to use a regex? Commented Apr 23, 2012 at 18:37
  • Not really, just figured that is whats most usually recommended Commented Apr 23, 2012 at 18:38
  • Will it always be same, i mean, add a dash(-) after every 4 characters? Commented Apr 23, 2012 at 18:39
  • Is the colouring on the '0Xj6' significant? Commented Apr 23, 2012 at 18:42
  • @Olly: It's because hexadecimal literals start with 0x. Commented Apr 23, 2012 at 18:43

11 Answers 11

33

Try Regex.Replace(input, @"(\w{4})(\w{4})(\w{4})", @"$1-$2-$3");

Regex is often derided, but is a pretty neat way of doing what you need. Can be extended to more complex requirements that are difficult to meet using string methods.

Sign up to request clarification or add additional context in comments.

3 Comments

Thats exactly what I was looking for, I know this can be done with substr etc but string concatenation often gets complex and is not very fun to write either :)
is there a way to say $1.toLower() or similar?
@Zanidd Yes, there's an overload of Replace() that allows you to use a custom function to generate the replacement string.
6

You can use "(.{4})(.{4})(.{4})" as your expression and "$1-$2-$3" as your replacement. This is, however, hardly a good use for regexp: you can do it much easier with Substring.

var res = s.Substring(0,4)+"-"+s.Substring(4,4)+"-"+s.Substring(8);

Comments

4

If the rule is to always split in three block of four characters no need for a reg exp:

str.Substring(0,4) + "-" + str.Substring(4,4) + "-" + str.Substring(8,4)

Comments

3

It would seem that a combination of String.Concat and string.Substring should take care of everything that you need.

Comments

2
  var str = "BPT4SH9R0XJ6";
  var newStr = str.Substring(0, 4) + "-" + str.Substring(4, 4) + "-" + str.Substring(8, 4);

Comments

2

Any reason you want to do a regex? you could just insert hyphens:

string s = "BPT4SH9R0XJ6";
for(int i = 4; i < s.length; i = i+5)
    s = s.Insert(i, "-");

This would keep adding hyphens every 4 characters, would not error out if string was too short/long/etc.

1 Comment

Are you sure the code is correct? You are not using the i variable. Also it's not very efficient since it creates new string object in every iteration.
1
return original_string.SubString(0,4)+"-"+original_string.SubString(4,4)+"-"+original_string.SubString(8,4);

Comments

1
string str = @"BPT4SH9R0XJ6";
string formattedString = string.Format("{0}-{1}-{2}", str.Substring(0, 4), str.Substring(4,4), str.Substring(8,4));

Comments

1

This works with any length of string:

            for (int i = 0; i < (int)Math.Floor((myString.Length - 1) / 4d); i++)
            {
                myString = myString.Insert((i + 1) * 4 + i, "-");
            }

Comments

0

Ended upp using this

var original = "BPT4SH9R0XJ6".ToCharArray();

var first = new string(original, 0, 4);
var second = new string(original, 4, 4);
var third = new string(original, 8, 4);
var mystring = string.Concat(first, "-", second, "-", third);

Thanks

Comments

0

If you are guaranteed the text you're operating on is the 12 character code then why don't you just use substring? Why do you need the Regex?

String theString = "AB12CD34EF56";
String theNewString = theString.Substring(0, 4) + "-" + theString.Substring(4, 4) + "-" + theString.Substring(8, 4);'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.