Skip to content

Commit 221a4ca

Browse files
authored
Merge pull request #8572 from MicrosoftDocs/main638016447403272007sync_temp
Repo sync for protected CLA branch
2 parents 61d5697 + 7a850c0 commit 221a4ca

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed
22.6 KB
Loading
5.81 KB
Loading

docs/ide/using-regular-expressions-in-visual-studio.md

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
---
22
title: Use regular expressions
33
description: Learn about some regular expression characters, operators, constructs, and pattern examples that you can use in Visual Studio.
4-
ms.custom: SEO-VS-2020
5-
ms.date: 10/21/2021
4+
ms.date: 10/14/2022
65
ms.topic: conceptual
76
f1_keywords:
87
- vsregularexpressionhelp
@@ -44,13 +43,13 @@ The following table contains some regular expression characters, operators, cons
4443
|Anchor the match string to the end of the file|$|`car$` matches "car" only when it appears at the end of the file|
4544
|Match any single character in a set|[abc]|`b[abc]` matches "ba", "bb", and "bc"|
4645
|Match any character in a range of characters|[a-f]|`be[n-t]` matches "bet" in "between", "ben" in "beneath", and "bes" in "beside", but finds no matches in "below"|
47-
|Capture and implicitly number the expression contained within parenthesis|()|`([a-z])X\1` matches "aXa"and "bXb", but not "aXb". "\1" refers to the first expression group "[a-z]". For more information, see [Capture groups and replacement patterns](#capture-groups-and-replacement-patterns). |
46+
|Capture and implicitly number the expression contained within parenthesis|()|`([a-z])X\1` matches "aXa" and "bXb", but not "aXb". "\1" refers to the first expression group "[a-z]". For more information, see [Capture groups and replacement patterns](#capture-groups-and-replacement-patterns). |
4847
|Invalidate a match|(?!abc)|`real(?!ity)` matches "real" in "realty" and "really" but not in "reality." It also finds the second "real" (but not the first "real") in "realityreal".|
49-
|Match any character that is not in a given set of characters. For more information, see [Negative character group](/dotnet/standard/base-types/character-classes-in-regular-expressions#negative-character-group-).|[^abc]|`be[^n-t]` matches "bef" in "before", "beh" in "behind", and "bel" in "below", but finds no matches in "beneath"|
48+
|Match any character that isn't in a given set of characters. For more information, see [Negative character group](/dotnet/standard/base-types/character-classes-in-regular-expressions#negative-character-group-).|[^abc]|`be[^n-t]` matches "bef" in "before", "beh" in "behind", and "bel" in "below", but finds no matches in "beneath"|
5049
|Match either the expression before or the one after the symbol|||`(sponge|mud) bath` matches "sponge bath" and "mud bath"|
5150
|[Escape the character](/dotnet/standard/base-types/character-escapes-in-regular-expressions) following the backslash| \\ |`\^` matches the character ^|
5251
|Specify the number of occurrences of the preceding character or group. For more information, see [Match exactly n times](/dotnet/standard/base-types/quantifiers-in-regular-expressions#match-exactly-n-times-n).|{n}, where 'n' is the number of occurrences|`x(ab){2}x` matches "xababx"<br/>`x(ab){2,3}x` matches "xababx" and "xabababx" but not "xababababx"|
53-
|[Match text in a Unicode category](/dotnet/standard/base-types/character-classes-in-regular-expressions#unicode-category-or-unicode-block-p). For more information about Unicode character classes, see [Unicode Standard 5.2 Character Properties](http://www.unicode.org/versions/Unicode5.2.0/ch04.pdf).|\p{X}, where "X" is the Unicode number.|`\p{Lu}` matches "T" and "D" in "Thomas Doe"|
52+
|[Match text in a Unicode category](/dotnet/standard/base-types/character-classes-in-regular-expressions#unicode-category-or-unicode-block-p). For more information about Unicode character classes, see [Unicode Standard 15.0 Character Properties](https://www.unicode.org/versions/Unicode15.0.0/ch04.pdf#G39).|\p{X}, where "X" is the Unicode number.|`\p{Lu}` matches "T" and "D" in "Thomas Doe"|
5453
|[Match a word boundary](/dotnet/standard/base-types/anchors-in-regular-expressions#word-boundary-b)|\b (Outside a character class `\b` specifies a word boundary, and inside a character class `\b` specifies a backspace.)|`\bin` matches "in" in "inside" but finds no matches in "pinto"|
5554
|Match a line break (that is, a carriage return followed by a new line)|\r?\n|`End\r?\nBegin` matches "End" and "Begin" only when "End" is the last string in a line and "Begin" is the first string in the next line|
5655
|Match any [word character](/dotnet/standard/base-types/character-classes-in-regular-expressions#word-character-w)|\w|`a\wd` matches "add" and "a1d" but not "a d"|
@@ -66,39 +65,39 @@ An example regular expression that combines some of the operators and constructs
6665

6766
A capture group delineates a subexpression of a regular expression and captures a substring of an input string. You can use captured groups within the regular expression itself (for example, to look for a repeated word), or in a replacement pattern. For detailed information, see [Grouping constructs in regular expressions](/dotnet/standard/base-types/grouping-constructs-in-regular-expressions).
6867

69-
To create a numbered capture group, surround the subexpression with parentheses in the regular expression pattern. Captures are numbered automatically from left to right based on the position of the opening parenthesis in the regular expression. To access the captured group:
68+
To create a numbered capture group, surround the subexpression with parentheses in the regular expression pattern. Captures are numbered automatically from left to right based on the position of the opening parenthesis in the regular expression. To access the captured group, consider the following examples:
7069

71-
- **within the regular expression**: Use `\number`. For example, `\1` in the regular expression `(\w+)\s\1` references the first capture group `(\w+)`.
70+
- **Within the regular expression**: Use `\number`. For example, `\1` in the regular expression `(\w+)\s\1` references the first capture group `(\w+)`.
7271

73-
- **in a replacement pattern**: Use `$number`. For example, the grouped regular expression `(\d)([a-z])` defines two groups: the first group contains a single decimal digit, and the second group contains a single character between **a** and **z**. The expression finds four matches in the following string: **1a 2b 3c 4d**. The replacement string `z$1` references the first group only (`$1`), and converts the string to **z1 z2 z3 z4**.
72+
- **In a replacement pattern**: Use `$number`. For example, the grouped regular expression `(\d)([a-z])` defines two groups: the first group contains a single decimal digit, and the second group contains a single character between **a** and **z**. The expression finds four matches in the following string: **1a 2b 3c 4d**. The replacement string `z$1` references the first group only (`$1`), and converts the string to **z1 z2 z3 z4**.
7473

7574
The following image shows a regular expression `(\w+)\s\1` and a replacement string `$1`. Both the regular expression and the replacement pattern reference the first capture group that's automatically numbered 1. When you choose **Replace all** in the **Quick Replace** dialog box in Visual Studio, repeated words are removed from the text.
7675

77-
![Quick Replace showing a numbered capture group in Visual Studio](media/numbered-capture-group.png)
76+
:::image type="content" source="media/numbered-capture-group.png" alt-text="Screenshot of Quick Replace showing a numbered capture group in Visual Studio.":::
7877

7978
> [!TIP]
80-
> Make sure the **Use Regular Expressions** button is selected in the **Quick Replace** dialog box.
79+
> In the **Quick Replace** dialog box, make sure to select the **Use Regular Expressions** button, or press **Alt**+**E**.
8180
8281
### Named capture groups
8382

8483
Instead of relying on the automatic numbering of a capture group, you can give it a name. The syntax for a named capture group is `(?<name>subexpression)`.
8584

86-
Named capture groups, like numbered capture groups, can be used within the regular expression itself or in a replacement pattern. To access the named capture group:
85+
Named capture groups, like numbered capture groups, can be used within the regular expression itself or in a replacement pattern. To access the named capture group, consider the following examples:
8786

88-
- **within the regular expression**: Use `\k<name>`. For example, `\k<repeated>` in the regular expression `(?<repeated>\w+)\s\k<repeated>` references the capture group that's named `repeated` and whose subexpression is `\w+`.
87+
- **Within the regular expression**: Use `\k<name>`. For example, `\k<repeated>` in the regular expression `(?<repeated>\w+)\s\k<repeated>` references the capture group that's named `repeated` and whose subexpression is `\w+`.
8988

90-
- **in a replacement pattern**: Use `${name}`. For example, `${repeated}`.
89+
- **In a replacement pattern**: Use `${name}`. For example, `${repeated}`.
9190

92-
As an example, the following image shows a regular expression `(?<repeated>\w+)\s\k<repeated>` and a replacement string `${repeated}`. Both the regular expression and the replacement pattern reference the capture group named `repeated`. When you choose **Replace all** in the **Quick Replace** dialog box in Visual Studio, repeated words are removed from the text.
91+
The following image shows a regular expression `(?<repeated>\w+)\s\k<repeated>` and a replacement string `${repeated}`. Both the regular expression and the replacement pattern reference the capture group named `repeated`. When you choose **Replace all** in the **Quick Replace** dialog box in Visual Studio, repeated words are removed from the text.
9392

94-
![Quick Replace showing a named capture group in Visual Studio](media/named-capture-group.png)
93+
:::image type="content" source="media/named-capture-group.png" alt-text="Screenshot of Quick Replace showing a named capture group in Visual Studio.":::
9594

9695
> [!TIP]
97-
> Make sure the **Use Regular Expressions** button is selected in the **Quick Replace** dialog box.
96+
> Make sure to select the **Use Regular Expressions** button (or press **Alt**+**E**) in the **Quick Replace** dialog box.
9897
9998
For more information about named capture groups, see [Named matched subexpressions](/dotnet/standard/base-types/grouping-constructs-in-regular-expressions#named-matched-subexpressions). For more information about regular expressions that are used in replacement patterns, see [Substitutions in regular expressions](/dotnet/standard/base-types/substitutions-in-regular-expressions).
10099

101100
## See also
102101

103-
- [Regular expression language](/dotnet/standard/base-types/regular-expression-language-quick-reference)
102+
- [Quick reference: Regular expression language](/dotnet/standard/base-types/regular-expression-language-quick-reference)
104103
- [Find and replace text](../ide/finding-and-replacing-text.md)

0 commit comments

Comments
 (0)