Turn -NotePad ++ regular expression substitution Advanced Usage

from Copy: NotePad ++ regular expression substitution Advanced Usage

Transfer from: http: //blog.csdn.net/gdp12315_gu/article/details/51730584

When we deal with files, often you will use Find and Replace. When we want to file a partial replacement of another part of the document replaced, how to do it? The following regular expression to provide me method.

Regular expressions provide sophisticated and flexible search and replace

Note: does not support multi-line expressions (involving \ n, \ r, etc).

1 Basic Expressions

symbol Explanation
. Matches any character except a new line (\ n). That is to say. "" Match \ r, when the document contains the same time \ r and \ n, the confusing. To match all the characters, use \ s \ S.
(…) This matches a tag area. This tag can be accessed by the syntax \ 1 accesses the first tag, \ 2 to access the second, similarly \ 3 \ 4 ... \ 9. These labels may be used in this regular expression, or for the search and replace the string change.
\1, \2, etc Representative label area 1-9 in the alternative (\ 1 to \ 9). For example, the search string Fred ([1-9]) XXX and replace string methods Sam \ 1YYY when Fred2XXX string found in the file is replaced Sam2YYY. Note: Only nine regions can be used, so we're safe when used, like \ 10 \ 2 represents a region 1 and the text "0" and the area 2.
[…] It represents a set of characters, such as [abc] represents any character a, b or c. We can also use the range, for example [az] Therefore, the lower case letters represent.
[^…] It represents a character complement. For example, [^ A-Za-z] represents any character except alphabet.
^ Start matching line (except in the collection, below).
$ Matching the line.
* Match 0 or more times, e.g. Sa * m matches Sm, Sam, Saam, Saaam like.
+ Matching one or more times, e.g. Sa + m matches Sam, Saam, Saaam like.
? Match 0 or 1, e.g. Sa? M matches Sm, Sam.
{n} Matching the determined n times. For example, 'Sa {2} m' matches Saam.
{m,n} Matching at least m times, up to n times (n if missing, then any number of times). For example, 'Sa {2,3} m' matches Saam or Saaam. 'Sa {2,} m' and 'Saa + m' same
*?, +?, ??, {n,m}? Non-greedy matching, matching the first valid match, usually '<>' will match the whole 'content' string - but '<?>' Matches only, "This marks a label areas, which you can use the syntax \ 1 \ 2, etc. corresponding to the plurality of access regions 1-9.

2 and a packet marking

symbol Explanation
(…) A set of capture may be by \ 1 accesses the first group, \ 2 to access the second.
(?:…) Non-capturing group.
(?=…) Non-capturing group - forward assertions such as '(= ton?) (*.)' Expression, when it comes to 'Appleton' string, it will match the 'Apple'..
(?<=…) Non-capturing group - rearward assertions e.g. '(<= sir?) (*.)' Represented by formula, when faced 'sir William' string match the 'William'..
(?!…) Non-capturing group - such as the expression of negative assertion forward, when faced with 'Apple', in addition to find each letter 'l', because it is followed by 'e'. '(?! e).'.
(? Non-capturing group - such as back negative assertions' (.?
(?P…) Named captured group. Submit a name to the group for subsequent use, for example, '(? PA [^ \ s] +) \ s (? P = first)' find 'Apple Apple'. Similar '(A [ ^ \ s] +) \ s \ 1 'with a group name rather than a number.
(?=name) Matching group named name. (? P ...).
(?#comment) Annotations - in brackets in the match will be ignored.

3 special symbols

symbol Explanation
\s Matching note the space, marking the end of the match will use [[: blank:]]. To avoid matching the new line.
\S Matching non-blank
\w Matching word character
\W Matches non-word character
\d Matching numeric characters
\D Matching non-numeric characters
\b Matching word boundaries. '\ BW \ w +' to find words that begin with W
\B 匹配非单词边界. ‘\Be\B+’ – 找到位于单子中间的字母’e’
\< This matches the start of a word using Scintilla’s definitions of words.
> This matches the end of a word using Scintilla’s definition of words.
\x 运行用x来表达可能具有其他意思的字符。例如, [ 用来插入到文本中作为[ 而不是作为字符集的开始.

4 字符类

符号 解释
[[:alpha:]] 匹配字母字符: [A-Za-z]
[[:digit:]] 匹配数字字符: [0-9]
[[:xdigit:]] 匹配16进制字符: [0-9A-Fa-f]
[[:alnum:]] 匹配字母数字字符: [0-9A-Za-z]
[[:lower:]] 匹配小写字符: [a-z]
[[:upper:]] 匹配大写字符: [A-Z]
[[:blank:]] 匹配空白 (空格 or tab):[ \t]
[[:space:]] 匹配空白字符:[ \t\r\n\v\f]
[[:punct:]] 匹配标点字符: [-!”#$%&’()*+,./:;<=>?@[]_`{
[[:graph:]] 匹配图形字符: [\x21-\x7E]
[[:print:]] 匹配可打印的字符 (graphical characters and spaces)
[[:cntrl:]] 匹配控制字符

5 替换操作

使用正则表达式的标记,通过()来包围想要用的字符,然后用\1 来替换字符串,第一个匹配文本。

例如:

Text body Search string Replace string Result
Hi my name is Fred my name is (.+) my name is not \1 Hi my name is not Fred
The quick brown fox jumped over the fat lazy dog brown (.+) jumped over the (.+) brown \2 jumped over the \1 The quick brown fat jumped over the fox lazy dog

6 限制

Support for regular expressions in PN2 is currently limited, the supported patterns and syntax are a very small subset of the powerful expressions supported by perl. 最大的限制是正则表达式只能匹配单行,不能用多行匹配表达。可以用Backslash Expressions代替.

准备计划是使用PCRE库 library (used elsewhere in PN2) 来支持文档搜索.

from http://www.pnotepad.org/docs/search/regular_expressions/

 

 

 

 

 

 

 

 

 

 

 

参考:

下面这个例子比较多

Notepad++的正则表达式替换和替换

 

Guess you like

Origin www.cnblogs.com/lizhaoxian/p/11260260.html