dotnet / dotnet/roslyn

Unexpected Displacement of RegEx Replacement Patterns when Inserting Braces

Open
#85,601 1 comment 0 reactions 0 assignees View on GitHub
Area-Razor
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

_This issue has been moved from [a ticket on Developer Community](https://developercommunity.visualstudio.com/t/Unexpected-Displacement-of-RegEx-Replace/10749361)._

---
[severity:It bothers me. A fix would be nice]
## Description
In Visual Studio, there's a bug where the RegEx replacement pattern "$0" in a `Regex.Replace` call is unintentionally moved when new curly braces are opened elsewhere in the code.

## Steps to Reproduce
1. Open a C# .razor file in Visual Studio.
2. Insert the following code:
```csharp
string result = Regex.Replace(input, ".{4}", "$0 ").Trim();
```
3. Place the cursor at any other location in the code.
4. Insert new curly braces, e.g., by typing "{}" and opening them.

## Expected Behavior
The original `Regex.Replace` call should remain unchanged.

## Actual Behavior
Visual Studio removes the "$0" from the `Regex.Replace` call and inserts it into the newly opened curly braces, changing the code to:
```csharp
string result = Regex.Replace(input, ".{4}", " ").Trim();
{$0}
```

## Additional Information
- Visual Studio Version: Enterprise 2022 17.11.4
- Affected file types: .razor
- Frequency: Occurs consistently with each brace insertion

---
### Original Comments

#### Feedback Bot on 9/19/2024, 03:01 PM:

This issue is currently being investigated. Our team will get back to you if either more information is needed, a workaround is available, or the issue is resolved.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.