1

I am trying to add a Tab space in a string which looks like this:

String 1: 1.1_1ATitle of the Chapter
String 2: 1.1_1Title of the Chapter

There is no space between "_1A" and "T". or between "_1" and "T".

The desired output is

1.1_1A      Title of the Chapter.
1.1_1       Title of the Chapter.

Here is what I tried:

string output=  Regex.Replace(input, "^([\\d.]+)", "$\t");

also

string output=  Regex.Replace(input, "^([\\d[A-Z]]+)", "$1 \t");

also

string output=  Regex.Replace(input, "^([\\d.]+)", "\\t");

Can I have a single Regex for both the inputs?

Many Thanks

1
  • @ClasG I have edited the question Commented Jun 1, 2016 at 8:02

1 Answer 1

1

You've complicated thing quite a bit with the introduction of a letter in the version/index (or whatever the first part is). You might get it to work with this though:

([\d._]+(?:[A-Z](?=[A-Z]))?)

(Note! No C escaping of \. Check ideone example for that.)

It grabs everything consisting of digits, dots and underscore. Then, in an optional non-capturing group, it matches (included in previous capture group) a capital letter, if it is followed by another capital letter (positive look-ahead).

This does however assume that the title always starts with a capital letter. I.e. if the numeric part is followed by two capital letters, it's assumed that the first is part of the numeric part.

Replace with $1\t to get desired effect.

See it here at ideone.

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

3 Comments

This is exactly what I was looking for!! Thank you so much!! I hope the question gets rid of the negative vote!
Glad to help. Feel free to up-vote, if, as it says on the up-vote arrow, "This answer is useful" ;)
I did up-vote, since I have less than 15 reputation points, the post score is not updated :(

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.