Question: I have a lot of string with following pattern(format):

```
aaaaaaa aa aa
bbbbbbbbbbbbbbb bb bbbbb bbb bb
ccccc c cc ccc 

XXXX XX 
zzzzzz zzz

```

OR:

```
aaaaaaa aa aa
bbbbbbbbbbbbbbb bb bbbbb bbb bb
ccccc c cc ccc

dddd  dddd 

XXXX XX 
zzzzzz zzz

```

OR :

```
aaaaaaa aa aa
bbbbbbbbbbbbbbb bb bbbbb bbb bb
ccccc c cc ccc

dddddddd

eeeee

XXXX XX 
zzzzzz zzz

```

I want to replace `XXXX XX` with `YYYY`. I think I need to detect last`Enter`in string and do the operation. How can I do this?

Answer: 
I'd do something like this. If the string in question is always on the second to last line, I'd split the string into an array of strings, a single string per line. Then find out how many lines (strings in array) there are. The object of interest is this number -2. Then replace this string with YYYY. 

**EDIT:**

```
var result = Regex.Split(input, "\r\n|\r|\n");
int len = result.Length;
result[len - 2] = "YYYY";
var output = string.Join(Environment.NewLine, result);

```
If it's just the pattern, here's an example with Regex:

```
\b\S{4}\s\S{2}\b

```

You could use this regex like this:

```
var regex = new Regex(@"\b\S{4}\s\S{2}\b");
var result = regex.Replace(inputString, "YYYY");

```

It looks for a word boundary (e.g. a return), then four non-whitespace characters, then one whitespace character, two non-whitespace characters and a word boundary again. It should do what you want.

However, depending on your input it might be a better idea to use this regex:

```
\b\S{4} \S{2}\b

```

So I replaced the whitespace character with an actual space character. Of course it could still happen that one of your characters is counted as a word boundary, then again I'd have to see an example of your input.

Here's an example of how it works:

[![example](https://i.stack.imgur.com/4MNTb.png)](https://i.stack.imgur.com/4MNTb.png)

It's in the C# interactive, which works pretty much the same as normal C#.

**EDIT**

As I realized that your pattern relevant line ends with a space, you could use this pattern as well:

```
\b\S{4} \S{2}\b\n

```

Which would probably work even better. However you'd have to replace it with `"YYYY\n"` then.