正则表达式修复 [英] regular expression Fix

查看:87
本文介绍了正则表达式修复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,
这是代码,可以说我需要在$ str字符串中更改单词"Brown",但要避免在span标签中将其写为颜色的位置对其进行更改,并避免锚点嵌套在其中它已经使用过,现在的问题是,此解决方案在最重要的位置不会改变,例如,正常的单词,如句子"brown browinsh charlie brown",此处开头和结尾的"brown"都应更改,但是它们arent:S:S ...有人为我做了这个正则表达式,但我无法修复:(

$ keyword =棕色";

$ str =快速的棕色狐狸跳过了< a href ="http://brown.com"> lazy</a"棕狗< p> akar bakar</p>.棕褐色< a href ="http://blahblah.com">布朗</a>布朗browinsh查理·布朗. ''这是\''棕色\''''. < b>布朗&b;外套";

$ href_str = preg_replace("/(& lt;)(\ w +)([^& gt< a] *& gt;)(".$ keyword.)/ie","``\\ 1''.''\\ 2''''\\ 3''.''< a href ="\" test.php \">< b>''.' '\\ 4''.''</b></a>'',$ str);

$ text_str = preg_replace("/([[& lt; \/\ w +& gt; |& lt; \ w +& gt;]])(*)(^< b> |& lt; \/b& gt;)(\ b.$ keyword." \ b)/ie,"''\\ 1.''\\ 2''.''< a href =" \"test.php \">''.''\\ 3''.''</a>'',$ href_str);

$ dquote_str = preg_replace("/(\")*(\ b.$ keyword." \ b)/ie," substr("\\ 0",1,1).''< a href ="\" test.php \">''.substr(''\\ 0'',2,strlen(''\\ 0'')-2).''</a>'' ,$ text_str);

$ squote_str = preg_replace("/(\'')*(\ b".$ keyword."\ b)/ie","substr(" \\ 0,0,1). href ="\" test.php \">''.substr(''\\ 0'',1,strlen(''\\ 0'')-1).''</a>' ',$ dquote_str);


echo $ squote_str;</b>

[修改:

@Kunal:仅添加预标签并不总是足够的.在完成之前,您需要实际查看它.因为$ str变量具有< ;,所以将其视为HTML且无法正确显示.

@arifeen:对我来说这看起来像php. (尤其是preg_replace).但是,如果是这种情况,那么您的字符串都将被弄乱,因为您没有在转义双引号(实际上,对此不确定).考虑到您使用的是正则表达式,我不想遍历所有的双引号,因为有时看起来不准确.
另外,在您粘贴时,它似乎已经转换了某些<转换为html格式(& lt;).我不知道这是否是故意的(我对此表示怀疑),但这看起来是错误的.我有太多问题想要尝试对其进行调整.]

Guys,
here is the code, lets say i need to change the word "Brown" in the $str string, but avoid changing it in in the where it is written as a color in the span tag, as well as avoid the nesting of anchors where its already used, now the problem is, this solution doesnt change where it is supposed to most importantly, i.e the normal words like in the sentence "brown browinsh charlie brown" here both "brown" in the start and end should change, but they arent :S:S ... someone did this regex for me, and i am unable to fix it :(

$keyword = "BROWN";

$str = "quick brown fox jumped over the <a href="http://brown.com">lazy</a> brownish dog <p>akar bakar</p>. brown brown <a href="http://blahblah.com">brown</a> brown browinsh charlie brown. " . ''This is \''brown\'''' . "<b>Brown</b> coat";

$href_str = preg_replace("/(&lt;)(\w+)([^&gt;^&lt;a]*&gt;)(".$keyword.")/ie", "''\\1''.''\\2''.''\\3''.''<a href="\"test.php\""><b>''.''\\4''.''</b></a>''", $str);

$text_str = preg_replace("/([&lt;\/\w+&gt;|&lt;\w+&gt;])( *)(^<b>|&lt;\/b&gt;)(\b".$keyword."\b)/ie", "''\\1''.''\\2''.''<a href="\"test.php\"">''.''\\3''.''</a>''", $href_str);

$dquote_str = preg_replace("/(\") *(\b".$keyword."\b)/ie", "substr(''\\0'', 1, 1). ''<a href="\"test.php\"">''. substr(''\\0'', 2, strlen(''\\0'') - 2).''</a>''", $text_str);

$squote_str = preg_replace("/(\'') *(\b".$keyword."\b)/ie", "substr(''\\0'', 0, 1). ''<a href="\"test.php\"">''. substr(''\\0'', 1, strlen(''\\0'') - 1).''</a>''", $dquote_str);


echo $squote_str ;</b>

[Modified:

@Kunal: just adding pre tags isn''t always enough. You need to actually look at it before finalizing it. Because the $str variable had <, it was treating it as HTML and not displaying properly.

@arifeen: This looks like php to me. (especially with the preg_replace). However, if that''s the case, then your strings are all messed up because you''re not escaping the double quotes (actually, not sure about that). With the fact that you''re using regular expressions, I didn''t want to go through and just escape all of the double quotes, because there were times that that seemed inaccurate.
Also, when you pasted, it seems to have converted some of the < to the html form (&lt;). I don''t know if that was intentional (I doubt it), but it looks wrong. There were just too many questions for me to try to adjust it.]

推荐答案

str字符串,但请避免在将其写为颜色的地方进行更改跨度标签,以及避免已使用的锚点嵌套,现在的问题是,此解决方案在最重要的位置不会改变,例如,普通的单词,如句子"brown browinsh charlie brown"开始和结束处的棕色"应该更改,但是它们不能:S:S ...有人为我执行了此正则表达式,而我无法修复它:(

str string, but avoid changing it in in the where it is written as a color in the span tag, as well as avoid the nesting of anchors where its already used, now the problem is, this solution doesnt change where it is supposed to most importantly, i.e the normal words like in the sentence "brown browinsh charlie brown" here both "brown" in the start and end should change, but they arent :S:S ... someone did this regex for me, and i am unable to fix it :(


关键字=棕色";

keyword = "BROWN";


str =快速的棕色狐狸跳过了< a href ="http://brown.com"&la; lazy</a>棕狗< p> ; akar bakar</p> ;.棕色棕色< a href ="http://blahblah.com">棕色</a>棕色布罗文什·查理·布朗. ``这是\''棕色\''''. < b>布朗&b;外套";

str = "quick brown fox jumped over the <a href="http://brown.com">lazy</a> brownish dog <p>akar bakar</p>. brown brown <a href="http://blahblah.com">brown</a> brown browinsh charlie brown. " . ''This is \''brown\'''' . "<b>Brown</b> coat";


这篇关于正则表达式修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆