Visual Studio 文本替换 正则表达式

<a href='http://www.omniture.com' title='Web Analytics'><img src='http://msstonojsmsdn.112.2o7.net/b/ss/msstonojsmsdn/1/H.20.2--NS/0' height='1' width='1' border='0' alt='' /></a>
Collapse All/Expand All Collapse All Expand All
Visual Studio
Regular Expressions (Visual Studio)

Regular expressions are a concise and flexible notation for finding and replacing patterns of text. A specific set of regular expressions can be used in the Find what field of the Visual Studio Find and Replace Window.

To enable the use of regular expressions in the Find what field during Quick Find, Find in Files, Quick Replace, or Replace in Files operations, select the Use option under Find Options and choose Regular expressions.

The triangular Expression Builder button next to the Find what field then becomes available. Click this button to display a list of the most commonly used regular expressions. When you choose any item from the Expression Builder, it is inserted into the Find what string.

NoteNote

There are syntax differences between the regular expressions that can be used in Find what strings and those that are valid in .NET Framework programming. For example, in Find and Replace, the braces notation {} is used for tagged expressions. So the expression zo{1} matches all occurrences of zo followed by the tag 1, as in Alonzo1 and Gonzo1. But within the .NET Framework, the notation {} is used for quantifiers. So the expression zo{1} matches all occurrences of z followed by exactly one o, as in zone but not in zoo.

Here are the regular expressions available in the Expression Builder.

ExpressionSyntaxDescription

Any character

.

Matches any single character except a line break.

Zero or more

*

Matches zero or more occurrences of the preceding expression, making all possible matches.

One or more

+

Matches at least one occurrence of the preceding expression.

Beginning of line

^

Anchors the match string to the beginning of a line.

End of line

$

Anchors the match string to the end of a line.

Beginning of word

<

Matches only when a word begins at this point in the text.

End of word

>

Matches only when a word ends at this point in the text.

Line break

\n

Matches a platform-independent line break. In a Replace expression, inserts a line break.

Any one character in the set

[]

Matches any one of the characters within the []. To specify a range of characters, list the starting and ending character separated by a dash (-), as in [a-z].

Any one character not in the set

[^...]

Matches any character not in the set of characters following the ^.

Or

|

Matches either the expression before or the one after the OR symbol (|). Mostly used within a group. For example, (sponge|mud) bath matches "sponge bath" and "mud bath."

Escape

\

Matches the character that follows the backslash (\) as a literal. This allows you to find the characters used in regular expression notation, such as { and ^. For example, \^ Searches for the ^ character.

Tagged expression

{}

Matches text tagged with the enclosed expression.

C/C++ Identifier

:i

Matches the expression ([a-zA-Z_$][a-zA-Z0-9_$]*).

Quoted string

:q

Matches the expression (("[^"]*")|('[^']*')).

Space or Tab

:b

Matches either space or tab characters.

Integer

:z

Matches the expression ([0-9]+).

The list of all regular expressions that are valid in Find and Replace operations is longer than can be displayed in the Expression Builder. You can also insert any of the following regular expressions into a Find what string:

ExpressionSyntaxDescription

Minimal — zero or more

@

Matches zero or more occurrences of the preceding expression, matching as few characters as possible.

Minimal — one or more

#

Matches one or more occurrences of the preceding expression, matching as few characters as possible.

Repeat n times

^n

Matches n occurrences of the preceding expression. For example, [0-9]^4 matches any 4-digit sequence.

Grouping

()

Allows you to group a set of expressions together. If you want to search for two different expressions in a single search, you can use the Grouping expression to combine them.

For example, if you want to search for - [a-z][1-3] or - [1-10][a-z], you would combine them: ([a-z][1-3]) | ([1-10][a-z]).

nth tagged text

\n

In a Find or Replace expression, indicates the text matched by the nth tagged expression, where n is a number from 1 to 9.

In a Replace expression, \0 inserts the entire matched text.

Right-justified field

\(w,n)

In a Replace expression, right-justifies the nth tagged expression in a field at least w characters wide.

Left-justified field

\(-w,n)

In a Replace expression, left-justifies the nth tagged expression in a field at least w characters wide.

Prevent match

~(X)

Prevents a match when X appears at this point in the expression. For example, real~(ity) matches the "real" in "realty" and "really," but not the "real" in "reality."

Alphanumeric character

:a

Matches the expression ([a-zA-Z0-9]).

Alphabetic character

:c

Matches the expression ([a-zA-Z]).

Decimal digit

:d

Matches the expression ([0-9]).

Hexadecimal digit

:h

Matches the expression ([0-9a-fA-F]+).

Rational number

:n

Matches the expression (([0-9]+.[0-9]*)|([0-9]*.[0-9]+)|([0-9]+)).

Alphabetic string

:w

Matches the expression ([a-zA-Z]+).

Escape

\e

Unicode U+001B.

Bell

\g

Unicode U+0007.

Backspace

\h

Unicode U+0008.

Tab

\t

Matches a tab character, Unicode U+0009.

Unicode character

\x#### or \u####

Matches a character given by Unicode value where #### is hexadecimal digits. You can specify a character outside the Basic Multilingual Plane (that is, a surrogate) with the ISO 10646 code point or with two Unicode code points giving the values of the surrogate pair.

The following table lists the syntax for matching by standard Unicode character properties. The two-letter abbreviation is the same as listed in the Unicode character properties database. These may be specified as part of a character set. For example, the expression [:Nd:Nl:No] matches any kind of digit.

ExpressionSyntaxDescription

Uppercase letter

:Lu

Matches any one capital letter. For example, :Luhe matches "The" but not "the".

Lowercase letter

:Ll

Matches any one lower case letter. For example, :Llhe matches "the" but not "The".

Title case letter

:Lt

Matches characters that combine an uppercase letter with a lowercase letter, such as Nj and Dz.

Modifier letter

:Lm

Matches letters or punctuation, such as commas, cross accents, and double prime, used to indicate modifications to the preceding letter.

Other letter

:Lo

Matches other letters, such as gothic letter ahsa.

Decimal digit

:Nd

Matches decimal digits such as 0-9 and their full-width equivalents.

Letter digit

:Nl

Matches letter digits such as roman numerals and ideographic number zero.

Other digit

:No

Matches other digits such as old italic number one.

Open punctuation

:Ps

Matches opening punctuation such as open brackets and braces.

Close punctuation

:Pe

Matches closing punctuation such as closing brackets and braces.

Initial quote punctuation

:Pi

Matches initial double quotation marks.

Final quote punctuation

:Pf

Matches single quotation marks and ending double quotation marks.

Dash punctuation

:Pd

Matches the dash mark.

Connector punctuation

:Pc

Matches the underscore or underline mark.

Other punctuation

:Po

Matches (,), ?, ", !, @, #, %, &, *, \, (:), (;), ', and /.

Space separator

:Zs

Matches blanks.

Line separator

:Zl

Matches the Unicode character U+2028.

Paragraph separator

:Zp

Matches the Unicode character U+2029.

Non-spacing mark

:Mn

Matches non-spacing marks.

Combining mark

:Mc

Matches combining marks.

Enclosing mark

:Me

Matches enclosing marks.

Math symbol

:Sm

Matches +, =, ~, |, <, and >.

Currency symbol

:Sc

Matches $ and other currency symbols.

Modifier symbol

:Sk

Matches modifier symbols such as circumflex accent, grave accent, and macron.

Other symbol

:So

Matches other symbols, such as the copyright sign, pilcrow sign, and the degree sign.

Other control

:Cc

Matches Unicode control characters such as TAB and NEWLINE.

Other format

:Cf

Formatting control character such as the bi-directional control characters.

Surrogate

:Cs

Matches one half of a surrogate pair.

Other private-use

:Co

Matches any character from the private-use area.

Other not assigned

:Cn

Matches characters that do not map to a Unicode character.

In addition to the standard Unicode character properties, the following additional properties may be specified as part of a character set.

ExpressionSyntaxDescription

Alpha

:Al

Matches any one character. For example, :Alhe matches words such as "The", "then", and "reached".

Numeric

:Nu

Matches any one number or digit.

Punctuation

:Pu

Matches any one punctuation mark, such as ?, @, ', and so on.

White space

:Wh

Matches all types of white space, including publishing and ideographic spaces.

Bidi

:Bi

Matches characters from right-to-left scripts such as Arabic and Hebrew.

Hangul

:Ha

Matches Korean Hangul and combining Jamos.

Hiragana

:Hi

Matches hiragana characters.

Katakana

:Ka

Matches katakana characters.

Ideographic/Han/Kanji

:Id

Matches ideographic characters, such as Han and Kanji.


Navigation

Regular Expressions (Visual Studio)
Community Content What is Community Content?
Add new contentRSS Annotations 
what sense does [1-10][a-z] make?fomar | Edit | Show History
Please Wait Please Wait
just to make clear: In the example for Grouping above you use the expression [1-10]. In fact this is equal to [10]. It does not mean 'look for a string that contains any number between 1 and 10 followed by a lowercase character from 'a' to 'z'', instead it looks for a string that contains '1' or '0' followed by a lowercase character. Otherwise you would need grouping itself to express 1-10, something like ([1-9]|10)[a-z] might do.

Regards
<input id="ctl00_WikiCont_ctl00_Editor_editorData" name="ctl00$WikiCont$ctl00$Editor$editorData" value="" just="" to="" make="" clear:="" in="" the="" example="" for="" grouping="" above="" you="" use="" expression="" [1-10].="" fact="" this="" is="" equal="" [10].="" it="" does="" not="" mean="" 'look="" a="" string="" that="" contains="" any="" number="" between="" 1="" and="" 10="" followed="" by="" lowercase="" character="" from="" 'a'="" 'z'',="" instead="" looks="" '1'="" or="" '0'="" character.="" otherwise="" would="" need="" itself="" express="" 1-10,="" something="" like="" ([1-9]|10)[a-z]="" might="" do.<br="" ><br="" >"="" type="hidden">
Prevent real~(ity) sample does not work in VS2008James in WI | Edit | Show History
Please Wait Please Wait
Visual Studio 2008 the "real~(ity)" sample does not work. It appear the prevent no longer works. Any thoughts?
Updating Html.Encode nuggets in MVC to ASP.NET 4 SyntaxSolidus667 | Edit | Show History
Please Wait Please Wait
Hey y'all. I'm using this Find expression:
\<%=:Wh@Html.Encode\({.*}\):Wh@%\>

and this Replace expression:
<%: \1 %>

to change this:
<%= Html.Encode(someObject.someField) %>

into this:
<%: someObject.someField %>

as I migrate my ASP.NET MVC app to ASP.NET MVC 2 on the .NET 4 Framework. I hope this helps you learn. It took me like 20 minutes to figure it out myself and I'd love to save someone that amount of time.

I use \ to escape the < and > symbols as well as the ( and ).
I use :Wh to denote whitespace and follow it up with @ to indicate that it might not occur at all or it might occur a lot.
I use {} to indicate the part I want to refer to in the replacement expression with \1.
Example of a regex replace found in the MSDN forums on Regular Expression1OmegaMan ... top1610 | Edit | Show History
Please Wait Please Wait
Check out the announcement post in MSDN Forums Regular Expression forum entitled: Visual Studio Regular Expressions at http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1175272&;;SiteID=1
backreferences == "tagged expressions"EvanTS3D | Edit | Show History
Please Wait Please Wait
"tagged expression" appears to be the Visual Studio term for a backreference.
Example of regex replace foundMaciej Greń | Edit | Show History
Please Wait Please Wait
Let's try to find following text:
fieldLabel: 'something'
text:'something2'

and repleace it with
fieldLabel: __('something')
text:__('something2')

to find it using Visual Studio Regex you have to use the following in "Find with" field:
(fieldLabel|text) *(:b)*\:*(:b):q

This means that you will search for existance of two words "fieldLabel" or "text". You also allow to have some space between "fieldLabel" and ":". Keep in mind that special characters like ":" has to be with backslash. the ":b" means is a shorthand which means that you allow a space or tab sign there.

To replace the search results with the proper string you have to add into your query tagged expressions. In our scenario:
{(fieldLabel|text)} *(:b)*\:*(:b){:q}

This way we are cutting out two values from our search results and we can use them in "replace with" field by using their indexes (for instance the first tagged expression {(fieldLabel|text)} has \1 index and the second {:q} has \2)

so the perform our find and replace scenarion you should have the following:
"find with"
{(fieldLabel|text)} *(:b)*\:*(:b){:q}

"replace with"
\1:__(\2)

More about it you can read in this blog post ( http://blog.goyello.com/2009/08/22/do-it-like-a-pro-–-visual-studio-find-and-replace/)
| Edit
Processing
Click to Rate and Give Feedback
Page view tracker
&amp;lt; div&amp;gt;&amp;lt;img alt=&quot;DCSIMG&quot; id=&quot;Img1&quot; width=&quot;1&quot; height=&quot;1&quot; src=&quot;http://m.webtrends.com/dcsmgru7m99k7mqmgrhudo0k8_8c6m/njs.gif?dcsuri=/nojavascript&amp;amp;WT.js=No&quot; /&amp;gt;&amp;lt;/div&amp;gt;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值