Home >CMS Tutorial >DEDECMS >How does Dreamweaver CMS search and replace the custom content of the database in batches through regular expressions?

How does Dreamweaver CMS search and replace the custom content of the database in batches through regular expressions?

藏色散人
藏色散人Original
2019-12-18 09:26:222574browse

How does Dreamweaver CMS search and replace the custom content of the database in batches through regular expressions?

织梦cms如何通过正则批量查找替换数据库的自定义内容?

正则的表达式在MY SQL中也有应用,但是在具体将查找的东西直接进行Replace的时候目前还不能解决,幸好后台中有个dede利用正则,利用dede正则批量替换数据库中内容,这样我们完全减小了自己的工作量了

推荐学习:织梦cms

  

例如网站的一个例子

是替换一个div中的内容,摘抄实践下,关键还是自己去体会,多用下正则,还是不错的、正则相关的文章我在我的博客中也多次提到了他的多种运用途径。如果你都掌握好它的话,你的工作效率就会成倍的提高了。

下面这个正则的用途是查找数据库的中自定义的DIV层中运用,一般这种用法你在采集,或者,用word直接添加内容后会用到。

看看这个是如何去除的。

例子一,去除简单自定义内容内容

选择数据表与字段:dede_addonarticle

替换方式:选择正则表达式

主键字段:aid

被替换内容:MM[\d]*GG

选择字段:body(这表是存储文章内容的),选择单选框:正则表达式,主键字段填写"aid",被替换内容为上面的正则:

例:MM[\d]*GG

替换为:可为空,也可填写你想置换为的内容。

替换条件:如果是全部,就不填写。开始测试时,先备份了数据库,然后仅替换一篇文章,条件中填写:aid=888,如果是分类,就填写:typeid=18,这样缩小替换范围,避免出错。

填写安全确认码,替换就ok了,

例子二,去除504e9fd2ed029434a1736f5b3a81869b*16b28748ea4df4d9c2150843fecfba68内容

<div style="background-color: #fff9ee; text-align: center"><font style="font-weight: bold;">文章地址址:<br 
/>//www.jb51.net/<br /></font> </div>

上述代码,是一个不错的style,大家可以显示下,调试效果不错。

可以利用以下正则来替换:

a00efa35b49d0284a2496b8a454ede4c]*\">(.*)e6e38b3c62e8df885fe2e3986461aa63 16b28748ea4df4d9c2150843fecfba68

在dede后台,选择:核心–数据库内容替换–选择表: 

选择数据表与字段:dede_addonarticle

替换方式:选择正则表达式

主键字段:aid

被替换内容:a00efa35b49d0284a2496b8a454ede4c]*\">(.*)e6e38b3c62e8df885fe2e3986461aa63 16b28748ea4df4d9c2150843fecfba68

选择字段:body(这表是存储文章内容的),选择单选框:正则表达式,主键字段填写"aid",被替换内容为上面的正则:

例:a00efa35b49d0284a2496b8a454ede4c]*\">(.*)e6e38b3c62e8df885fe2e3986461aa63 16b28748ea4df4d9c2150843fecfba68

替换为:可为空,也可填写你想置换为的内容。

替换条件:如果是全部,就不填写。 

注意:开始测试时,先备份了数据库,然后仅替换一篇文章,条件中填写:aid=8880,如果是分类,就填写:typeid=18,这样缩

小替换范围,避免出错。

填写安全确认码,替换就ok了,注意这个安全确认码会有失效周期,但它自己不提示,也有这个可能,会选择替换无反应,遇见

这情况,只有刷新当前页面了。

很完整了吧!其它正则参考:http://bbs.dedecms.com/214856.html,这个当中的也不完整,也不太正确,需要对比来修改调试

,很费时间。 

正则是个好技术,有着很强大的应用,花了点时间看,你的工作效果会大陪的提高!

这是我在处理文章时用到一些正则表达式,里面有你要的。在Editplus中使用。

在表:dede_addonarticle中,body字段即是文章内容。

过滤Table相关的:

686c1c7e8f5b1b987d6d9cc0528b910a]*)>|f16b1740fad44fb09bfe928bcc527e08|b6c5a531a458a2e790c1fd6421739d1c|b90dd5946f0946207856a8a37f441edf|92cee25da80fac49f6fb6eec5fd2c22a|ca745a59da05f784b8811374296574e1|a34de1251f0d9fe1e645927f19a896e8|fd273fcf5bcad3dfdad3c41bd81ad3e5 

正则: width=\"[0-9][0-9][0-9]\"| width=[0-9][0-9][0-9]| height=\"[0-9][0-9][0-9]\"| height=[0-9][0-9][0-9],

说明:过虑height、Width。 

正则:a00efa35b49d0284a2496b8a454ede4c]*\">|a8dafd4a2998db01cadce6e4666309df]*>|16b28748ea4df4d9c2150843fecfba68|dc6dce4a544fdca2df29d5ac0ea9906b, note: matches 3ce2f59fcf347898c75719ab0f416255 or 4f896cde7acb3fcdb85abb62bc81dca2.

Regular: style=\"[^"]*\"| style=[^"]*, note: Match Style="border:"

Regular: 123af2c36a4505ae61ed7a51535ebb46]*)>(.*)531ac245ce3e4fe3d50054a55f265927, note: matching 080b747a20f9163200dd0a7d304ba388531ac245ce3e4fe3d50054a55f265927

regular: afcd91120f0a98fe08b40bf812b4c0de]*>|240cb830ca84ebaabbd07850110b414d|e6e38b3c62e8df885fe2e3986461aa63|783809872483f75416d3ff594df013fb]*>|8e99a69fbe029cd4e2b854e244eab143|128dba7a3a77be0113eb0bea6ea0a5d0 , note: matches 240cb830ca84ebaabbd07850110b414d and 8e99a69fbe029cd4e2b854e244eab143

Regular: border="[0-9]"| border=[0-9], note: matching border=""

Regular: b7c0e2d89f459e6feeb57f28c78b0e32]*>|45a2772a6b6107b401db3c9b82c049c2|54bdf357c58b8a65c66d7c19c8e4d114 , note: matching 45a2772a6b6107b401db3c9b82c049c2

regular: id=[^ ]*| id=\"[^ ]*\" , note: matching id=""

regular: title=[0-9][^ ]*| title=\"[0-9][^ ]*\"| alt=[0-9 ][^ ]*| alt=\"[0-9][^ ]*\"| alt=[a-z][^ ]*| alt=\"[a

-z][^ ]* \", Note: Match Title or alt

Regular: 699014ab8e179d07c90b014c392d5429]*–>, Description: Match HTML comment

Regular: a87b874476f44350b4b2124e5f9aeeb2]*)>(.*)2cacc6d41bbb37262a98f745aa00fbf0, Description: Match all content between 3f1c4e4b6b16bbbd69b2ee476dc4f83a2cacc6d41bbb37262a98f745aa00fbf0

Regular: class=[a-z][^ | ^>]*| class=\"[a-z][^ |^>]*\", description: Clean up class="", fully tested

Regular:23bedc0460c8b0c0e0dad0f336bebc85|f16b1740fad44fb09bfe928bcc527e08|b6c5a531a458a2e790c1fd6421739d1c|b90dd5946f0946207856a8a37f441edf|92cee25da80fac49f6fb6eec5fd2c22a|ca745a59da05f784b8811374296574e1|a34de1251f0d9fe1e645927f19a896e8|fd273fcf5bcad3dfdad3c41bd81ad3e5

Regular: 5862b3944ec158707880f35ec6042200]*>|c1a436a314ed609750bd7c7d319db4da|2e9b454fa8428549ca2e64dfac4625cd| align=left|907fae80ddef53131f3292ee4f81644b|d1c6776b927dc33c5d9114750b586338|bacbf9e1ad7f40415ce1670e31edfee3|c58f81670e4eeff2e2931f0a3335c4c3]*)>|5db79b134e9f6b82c0b36e0489ee08ed

Regular expression is a universal standard, and most computer languages ​​support regular expressions, including as3, Here are some commonly used regular expression statements, so you don’t have to write them yourself when you use them

^[0-9]*[1-9][0-9]*$ //Matching Positive integer

^-[0-9]*[1-9][0-9]*$ //Match negative integer

^-?\d $ //Match integer

^\d (\.\d )?$  //Match non-negative floating point numbers (positive floating point numbers 0)

^(([0-9] \.[0-9] *[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9] )|([0-9]*[1 -9][0-9]*))$ //Match positive floating point numbers

^((-\d (\.\d )?)|(0 (\.0 )?))$ //Match non-positive floating point numbers (negative floating point number 0)

^(-(([0-9] \.[0-9]*[1-9][0-9]*)| ([0-9]*[1-9][0-9]*\.[0-9] )|([0-9]*[1-9][0-9]*)))$ / /Match negative floating point numbers

^(-?\d )(\.\d )?$ //Match floating point numbers

^[A-Za-z] $ //Match by A string of 26 English letters

^[A-Z] $ // Matches a string of 26 uppercase letters

^[a-z] $ // Matches a string of 26 uppercase letters A string consisting of lowercase English letters

^[A-Za-z0-9] $ //Match a string consisting of numbers and 26 English letters

^\w $ / / Matches a string consisting of numbers, 26 English letters or underscores

^[\w-] (\.[\w-] )*@[\w-] (\.[\w- ] ) $ //Match email address

^[a-zA-z] ://Match (\w (-\w )*)(\.(\w (-\w )*)) *(\?\S*)?$ //Match url

Regular expression matching Chinese characters: [\u4e00-\u9fa5]

Match double-byte characters (including Chinese characters in (within): [^\x00-\xff]

Regular expression matching blank lines:\n[\s| ]*\r

Regular expression matching HTML tags:/ db05f8e866f47612e82f5cb19e67888c.*0e1c620fd1f13d4254a0bb23aec420ea|b1ca7e9394ce1dfc0ddfd739020cabe6/

Regular expression matching leading and trailing spaces: (^\s*)|( \s*$)

Regular expression matching email addresses: \w ([- .]\w )*@\w ([-.]\w )*\.\w ([-. ]\w )*

Regular expression matching URL: ^[a-zA-z] ://(\w (-\w )*)(\.(\w (-\w) )*))*(\?\S*)?$

Is the matching account legal (starting with a letter, 5-16 bytes allowed, alphanumeric underscores allowed): ^[a-zA-Z][ a-zA-Z0-9_]{4,15}$

Matches domestic phone numbers: (\d{3}-|\d{4}-)?(\d{8}|\d {7})?

Matches Tencent QQ number: ^[1-9]*[1-9][0-9]*$

The following table shows the metacharacters and their regular expressions A complete list of behaviors in the context of an expression:

\ Marks the next character as a special character, or a literal character, or a backreference, or an octal escape character.

^ Matches the beginning of the input string. If the RegExp object's Multiline property is set, ^ also matches the position after '\n' or '\r'.

$ Matches the end position of the input string. If the Multiline property of the RegExp object is set, $ also matches the position before '\n' or '\r'.

* Matches the preceding subexpression zero or more times.

Matches the previous subexpression one or more times. Equivalent to {1,}.

? Matches the preceding subexpression zero or one time. ? Equivalent to {0,1}.

{n} n 是一个非负整数,匹配确定的n 次。 

{n,} n 是一个非负整数,至少匹配n 次。 

{n,m} m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。在逗号和两个数之间不能有空格。 

? 当该字符紧跟在任何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹

配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串。 

. 匹配除 "\n" 之外的任何单个字符。要匹配包括 '\n' 在内的任何字符,请使用象 '[.\n]' 的模式。 

(pattern) 匹配pattern 并获取这一匹配。 

(?:pattern) 匹配pattern 但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。

(?=pattern) 正向预查,在任何匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要

获取供以后使用。 

(?!pattern) 负向预查,与(?=pattern)作用相反 

x|y 匹配 x 或 y。 

[xyz] 字符集合。 

[^xyz] 负值字符集合。 

[a-z] 字符范围,匹配指定范围内的任意字符。 

[^a-z] 负值字符范围,匹配任何不在指定范围内的任意字符。 

\b 匹配一个单词边界,也就是指单词和空格间的位置。 

\B 匹配非单词边界。 

\cx 匹配由x指明的控制字符。 

\d 匹配一个数字字符。等价于 [0-9]。 

\D 匹配一个非数字字符。等价于 [^0-9]。 

\f 匹配一个换页符。等价于 \x0c 和 \cL。 

\n 匹配一个换行符。等价于 \x0a 和 \cJ。 

\r 匹配一个回车符。等价于 \x0d 和 \cM。 

\s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于[ \f\n\r\t\v]。 

\S 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。 

\t 匹配一个制表符。等价于 \x09 和 \cI。 

\v 匹配一个垂直制表符。等价于 \x0b 和 \cK。 

\w 匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'。 

\W 匹配任何非单词字符。等价于 '[^A-Za-z0-9_]'。 

\xn 匹配 n,其中 n 为十六进制转义值。十六进制转义值必须为确定的两个数字长。 

\num 匹配 num,其中num是一个正整数。对所获取的匹配的引用。 

\n 标识一个八进制转义值或一个后向引用。如果 \n 之前至少 n 个获取的子表达式,则 n 为后向引用。否则,如果 n 为八进

制数字 (0-7),则 n 为一个八进制转义值。 

\nm 标识一个八进制转义值或一个后向引用。如果 \nm 之前至少有is preceded by at least nm 个获取得子表达式,则 nm 为

后向引用。如果 \nm 之前至少有 n 个获取,则 n 为一个后跟文字 m 的后向引用。如果前面的条件都不满足,若 n 和 m 均为

八进制数字 (0-7),则 \nm 将匹配八进制转义值 nm。 

\nml 如果 n 为八进制数字 (0-3),且 m 和 l 均为八进制数字 (0-7),则匹配八

关于批量替换 正则表达式

这是我在处理文章时用到一些正则表达式,里面有你要的。在Editplus中使用。

在表:dede_addonarticle中,body字段即是文章内容。

过滤Table相关的:

686c1c7e8f5b1b987d6d9cc0528b910a]*)>|f16b1740fad44fb09bfe928bcc527e08|b6c5a531a458a2e790c1fd6421739d1c|b90dd5946f0946207856a8a37f441edf|92cee25da80fac49f6fb6eec5fd2c22a|ca745a59da05f784b8811374296574e1|a34de1251f0d9fe1e645927f19a896e8|fd273fcf5bcad3dfdad3c41bd81ad3e5

还有一些,可供你参考,我加了一些注释,发个贴太累了,哎~~~自己摸索吧:

正则: width=\"[0-9][0-9][0-9]\"| width=[0-9][0-9][0-9]| height=\"[0-9][0-9][0-9]\"| height=[0-9][0-9][0-9],说明:过虑height、Width。

------------------------------------------------------

正则:a00efa35b49d0284a2496b8a454ede4c]*\">|a8dafd4a2998db01cadce6e4666309df]*>|16b28748ea4df4d9c2150843fecfba68|dc6dce4a544fdca2df29d5ac0ea9906b, note: matches 3ce2f59fcf347898c75719ab0f416255 or 4f896cde7acb3fcdb85abb62bc81dca2.

-------------------------------------------------- ----------

Regular: style=\"[^"]*\"| style=[^"]*, note: Match Style="border:"

-------------------------------------------------- -------

Regular: 3ce07832cb440bb0fe1a341742064417]*)>(.*)531ac245ce3e4fe3d50054a55f265927, note: matches 11c6b9a105a1d9ddb712796d6febda24531ac245ce3e4fe3d50054a55f265927

------------------------------------ ------------------

Regular: 7e9a3d2c1887476e4b33386235d9e759]*>|240cb830ca84ebaabbd07850110b414d|e6e38b3c62e8df885fe2e3986461aa63|< ;strong [^>]*>|8e99a69fbe029cd4e2b854e244eab143|128dba7a3a77be0113eb0bea6ea0a5d0 , note: matches 240cb830ca84ebaabbd07850110b414d and 8e99a69fbe029cd4e2b854e244eab143

--------- --------------------------------------------------

Regular: border="[0-9]"| border=[0-9], note: match border=""

----------------- -------------------------------------

Regular: b7c0e2d89f459e6feeb57f28c78b0e32]*>|45a2772a6b6107b401db3c9b82c049c2|54bdf357c58b8a65c66d7c19c8e4d114 , note: matches 45a2772a6b6107b401db3c9b82c049c2

------------------- ----------------------------------

Regular: id=[^ ]*| id=\"[^ ]*\", note: match id=""

-------------------------- ----------------------------

Regular: title=[0-9][^ ]*| title= \"[0-9][^ ]*\"| alt=[0-9][^ ]*| alt=\"[0-9][^ ]*\"| alt=[a-z][^ ] *| alt=\"[a-z][^ ]*\", note: match Title or alt

--------------------- --------------------------------

Regular: 2de7396c38fb34bc42fc5741409b96e3] *-->, Description: Match HTML comments

--------------------------------- ---------------------

Regular: dab35ffe1f80f74b600acfe571757051]*)>(.*)

------------------------ -----------------------------

Regular: class=[a-z][^ |^>]* | class=\"[a-z][^ |^>]*\", description: Clean up class="", fully tested

-------------- ----------------------------------------

Regular: abd985068db55b852e075ae67262f413]*)>|f16b1740fad44fb09bfe928bcc527e08|b6c5a531a458a2e790c1fd6421739d1c|b90dd5946f0946207856a8a37f441edf|92cee25da80fac49f6fb6eec5fd2c22a|ca745a59da05f784b8811374296574e1|a34de1251f0d9fe1e645927f19a896e8|fd273fcf5bcad3dfdad3c41bd81ad3e5

-------------------------------------------------- ----------

Regular: 5862b3944ec158707880f35ec6042200]*>|c1a436a314ed609750bd7c7d319db4da|2e9b454fa8428549ca2e64dfac4625cd| align=left|907fae80ddef53131f3292ee4f81644b| d1c6776b927dc33c5d9114750b586338|bacbf9e1ad7f40415ce1670e31edfee3|adca8a5fa06ffeafb062c2e3f274b930|046f2828e9bf995c0ed22c85796302a5]*)>|5db79b134e9f6b82c0b36e0489ee08ed

The above is the detailed content of How does Dreamweaver CMS search and replace the custom content of the database in batches through regular expressions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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