You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/ide/using-regular-expressions-in-visual-studio.md
+16-17Lines changed: 16 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,7 @@
1
1
---
2
2
title: Use regular expressions
3
3
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
6
5
ms.topic: conceptual
7
6
f1_keywords:
8
7
- vsregularexpressionhelp
@@ -44,13 +43,13 @@ The following table contains some regular expression characters, operators, cons
44
43
|Anchor the match string to the end of the file|$|`car$` matches "car" only when it appears at the end of the file|
45
44
|Match any single character in a set|[abc]|`b[abc]` matches "ba", "bb", and "bc"|
46
45
|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). |
48
47
|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"|
50
49
|Match either the expression before or the one after the symbol|||`(sponge|mud) bath` matches "sponge bath" and "mud bath"|
51
50
|[Escape the character](/dotnet/standard/base-types/character-escapes-in-regular-expressions) following the backslash|\\|`\^` matches the character ^|
52
51
|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"|
54
53
|[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"|
55
54
|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|
56
55
|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
66
65
67
66
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).
68
67
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:
70
69
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+)`.
72
71
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**.
74
73
75
74
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.
76
75
77
-

76
+
:::image type="content" source="media/numbered-capture-group.png" alt-text="Screenshot of Quick Replace showing a numbered capture group in Visual Studio.":::
78
77
79
78
> [!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**.
81
80
82
81
### Named capture groups
83
82
84
83
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)`.
85
84
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:
87
86
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+`.
89
88
90
-
-**in a replacement pattern**: Use `${name}`. For example, `${repeated}`.
89
+
-**In a replacement pattern**: Use `${name}`. For example, `${repeated}`.
91
90
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.
93
92
94
-

93
+
:::image type="content" source="media/named-capture-group.png" alt-text="Screenshot of Quick Replace showing a named capture group in Visual Studio.":::
95
94
96
95
> [!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.
98
97
99
98
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).
0 commit comments