Home > Development Tools > notepad > body text

How to replace regular expressions in NotePad++ (picture and text)

不言
Release: 2018-09-26 16:38:42
Original
14654 people have browsed it

 本篇文章给大家带来的内容是关于NotePad++正则表达式如何进行替换(图文),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

NotePad++ 正则表达式替换 高级用法

const getQAPartnerSites = (params) => wxRequest(params, apiUrlApp + 'ask/show_sites/')
const getQARecommender = (params) => wxRequest(params, apiUrlApp + 'ask/question_likely/')
const postQAurls = (params) => wxRequest(params, apiUrlApp + 'ad/ad_pub/')
const getQAPublished = (params) => wxRequest(params, apiUrlApp + 'ask/ask_list/')
Copy after login
^const\W{1}(\w+)\W{1}\={1}.+$
\1,
Copy after login
getQAPartnerSites,
getQARecommender,
postQAurls,
getQAPublished,
Copy after login

在我们处理文件时,很多时候会用到查找与替换。当我们想将文件中某一部分替换替换文件中另一部分时,怎么办呢? 下面正则表达式 给我提供方法。

正则表达式,提供复杂 并且弹性的查找与替换

注意: 不支持多行表达式 (involving \n, \r, etc).

1 基本表达式

符号 解释
. 匹配任意字符,除了新一行(\n)。也就是说 “.”可以匹配 \r ,当文件中同时含有\r and \n时,会引起混乱。要匹配所有的字符,使用\s\S。
(…) 这个匹配一个标签区域. 这个标签可以被访问,通过语法 \1访问第一个标签, \2 访问第二个, 同理 \3 \4 … \9。 这些标签可以用在当前正则表达式中,或则替search和replace中的换字符串。
\1, \2, etc 在替换中代表1到9的标签区域(\1 to \9)。例如, 查找字符串 Fred([1-9])XXX 并替换为字符串 Sam\1YYY的方法,当在文件中找到Fred2XXX的字符串时,会替换为Sam2YYY。注意: 只有9个区域能使用,所以我们在使用时很安全,像\10\2 表示区域1和文本”0”以及区域2。
[…] 表示一个字符集合, 例如 [abc]表示任意字符 a, b or c.我们也可以使用范围例如[a-z] 表示所以的小写字母。
[^…] 表示字符补集. 例如, [^A-Za-z] 表示任意字符除了字母表。
^ 匹配一行的开始(除非在集合中, 如下).
$ 匹配行尾.
* 匹配0或多次, 例如 Sa*m 匹配 Sm, Sam, Saam, Saaam 等等.
+ 匹配1次或多次,例如 Sa+m 匹配 Sam, Saam, Saaam 等等.
? 匹配0或者1次, 例如 Sa?m 匹配 Sm, Sam.
{n} 匹配确定的 n 次.例如, ‘Sa{2}m’ 匹配 Saam.
{m,n} 匹配至少m次,至多n次(如果n缺失,则任意次数).例如, ‘Sa{2,3}m’ 匹配 Saam or Saaam. ‘Sa{2,}m’ 与 ‘Saa+m’相同
*?, +?, ??, {n,m}? 非贪心匹配,匹配第一个有效的匹配,通常 ‘<.>’ 会匹配整个 ‘content’字符串 –但 ‘<.?>’ 只匹配 ” .这个标记一个标签区域,这些区域可以用语法\1 \2 等访问多个对应1-9区域。

2 标记和分组

符号 解释
(…) 一组捕获. 可以通过\1 访问第一个组, \2 访问第二个.
(?:…) 非捕获组.
(?=…) 非捕获组 – 向前断言. 例如’(.*)(?=ton)’ 表达式,当 遇到’Appleton’字符串时,会匹配为’Apple’.
(?<=…)非捕获组 – 向后断言. 例如’(?<=sir) (.*)’ 表示式,当遇到’sir William’ 字符串时,匹配为’ William’.
(?!…)非捕获组 – 消极的向前断言. 例如’.(?!e)’ 表达式,当遇到’Apple’时,会找到每个字母除了 ‘l’,因为它紧跟着 ‘e’.
(?非捕获组 – 消极向后断言. 例如 ‘(?
(?P…)命名所捕获的组. 提交一个名称到组中供后续使用,例如’(?PA[^\s]+)\s(?P=first)’ 会找到 ‘Apple Apple’. 类似的 ‘(A[^\s]+)\s\1’ 使用组名而不是数字.
(?=name)匹配名为name的组. (?P…).
(?#comment)批注 –括号中的内容在匹配时将被忽略。

3 Special symbols

SymbolExplanation
\s Matches spaces. Note that the end of the tag will be matched. Use [[:blank:]] to avoid matching new lines.
\SMatches non-whitespace
\wMatches word characters
\W Matches non-word characters
\d Matches numeric characters
\DMatch non-numeric characters
\bMatch word boundaries. '\bW\w ' Find words starting with W
\B Matches non-word boundaries. '\Be\B ' – Finds the letter 'e' in the middle of a monad
\<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 runs using x to express characters that may have other meanings. For example, [ is used to insert into text as [ rather than as the beginning of a character set.

4 Character Classes

##[[:alpha:]]Match alphabetical characters: [A-Za- z][[:digit:]] Matches numeric characters: [0-9][[ :xdigit:]] Matches hexadecimal characters: [0-9A-Fa-f][[:alnum:]]Match alphanumeric characters: [0-9A-Za-z][[:lower:]]Match lowercase characters: [a-z][[:upper:]] Matches uppercase characters: [A-Z][[:blank:]]Match white space (space or tab):[ \t][[:space:]]Match white space characters:[ \t\r \n\v\f][[:punct:]] Matches punctuation characters: [-!”#$%&'()* ,. /:;<=>?@[]_`{[[:graph:]]Match graphic characters: [\x21-\x7E ][[:print:]] Matches printable characters (graphical characters and spaces)[ [:cntrl:]]Match control characters
Symbol Explanation
5 Replacement operation

Use regular expression tags through () Surround the desired characters, then replace the string with \1, the first one matching text.

For example:

Text bodySearch stringReplace stringResult##Hi my name is Fred##The quick brown fox jumped over the fat lazy dogbrown (. ) jumped over the (. )brown \2 jumped over the \1The quick brown fat jumped over the fox lazy dog6 Limitations
my name is (. ) my name is not \1 Hi my name is not Fred
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. The largest The limitation is that regular expressions can only match a single line and cannot be used to match multiple lines. Backslash Expressions can be used instead.

The plan is to use the PCRE library (used elsewhere in PN2) to support document search.

The above is the detailed content of How to replace regular expressions in NotePad++ (picture and text). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template