0

I have the following string which contains reviews for products, I want to move sentences like 1 of 3 and 4 of 13 into new lines

Input string

The mapping features have a lot of inaccuracie 1 of 3
am a little disappointed in the new 4S. 4 of 13

Output string

The mapping features have a lot of inaccuracies 
1 of 3
am a little disappointed in the new 4S 
4 of 13

I was trying Regex.Replace because it changes all occurrences in the string

I located the string using @"\d+ of \d+"

but how can I keep the variable number in the replacement text? Or can you suggest a different method?

4

1 回答 1

2

You need to capture the match in order to be able to use it for replacement:

@"(\d+ of \d+)"

To replace this value, use $1.

Something like:

Regex.Replace(input, 
              @"(\d+ of \d+)", 
              string.Format(@"{0}$1{0}", Environment.NewLine))
于 2012-05-05T08:55:58.300 回答