说明:
分隔符 (),/,//
(a) b : b,a b 均正确.括号里的词可有可无
(a/b) c : c,a c,b c 均正确./前后的词是或的关系
a//b : a,b均正确
要求:
把以上符号表示的词语组合转换成正则表示法
例:((it is/’s) the/a) cat // (It is/’s) (the) animal
回复讨论(解决方案)
说明:
分隔符 (),/,//
(a) b : b,a b 均正确.括号里的词可有可无
(a/b) c : c,a c,b c 均正确./前后的词是或的关系
a//b : a,b均正确
要求:
把以上符号表示的词语组合转换成正则表示法
例:((it is/’s) the/a) cat // (It is/’s) (the) animal
不太理解你要表达的意思
首先(a)b匹配的是字符“ab”,子字符串“a”
其次(a/b)c匹配的是字符“a/bc”,子字符串“a/b”
再次a//b匹配的是字符串“a//b”
((it is/’s) the/a) cat // (It is/’s) (the) animal
你是想写正则表达式匹配
it is a cat
it is the cat
it's a cat
it's the cat
it is animal
it is the animal
it's animal
it's the animal
吧?
(it('s|\s+is)\s+(a|the)\s+cat)|(it('s|\s+is)\s+(animal|the\s+animal))
不太理解你要表达的意思
首先(a)b匹配的是字符“ab”,子字符串“a”
其次(a/b)c匹配的是字符“a/bc”,子字符串“a/b”
再次a//b匹配的是字符串“a//b”
((it is/’s) the/a) cat // (It is/’s) (the) animal
你是想写正则表达式匹配
it is a cat
it is the cat
it's a cat
it's the cat
it is animal
it is the animal
it's animal
it's the animal
吧?
除了上面这些,
cat
a cat
the cat
animal
the animal
都是对的。
我要一种转化方式,分隔符 (),/,// 这些是约定规则,并非正则语意.感谢1楼回复
做个事例便于理解
function pregRule($str){ $str = trim($str); return '/^'.str_replace(array(') ','[',']','//','/',' '),array(')?\s*','(','){1}','|','|','\s+'),$str).'$/'; }$str = "(a) b";echo pregRule($str).'<br>';$str = "[a//b]"; #加个中括号,方便转换echo pregRule($str).'<br>';$str = "(a/b) c";echo pregRule($str).'<br>';$str = "((it [is/’s]) the/a) cat // (It [is/’s]) (the) animal";echo pregRule($str).'<br>';
#result:
/^(a)?\s*b$/
/^(a|b){1}$/
/^(a|b)?\s*c$/
/^((it\s+(is|’s){1})?\s*the|a)?\s*cat\s+|\s+(It\s+(is|’s){1})?\s*(the)?\s*animal$/
前三种还凑合,第四个匹配就不忍心看了,请大家帮忙丰富这个函数,使之适应,可以适当修改语句
做个事例便于理解
function pregRule($str){ $str = trim($str); return '/^'.str_replace(array(') ','[',']','//','/',' '),array(')?\s*','(','){1}','|','|','\s+'),$str).'$/'; }$str = "(a) b";echo pregRule($str).'<br>';$str = "[a//b]"; #加个中括号,方便转换echo pregRule($str).'<br>';$str = "(a/b) c";echo pregRule($str).'<br>';$str = "((it [is/’s]) the/a) cat // (It [is/’s]) (the) animal";echo pregRule($str).'<br>';
#result:
/^(a)?\s*b$/
/^(a|b){1}$/
/^(a|b)?\s*c$/
/^((it\s+(is|’s){1})?\s*the|a)?\s*cat\s+|\s+(It\s+(is|’s){1})?\s*(the)?\s*animal$/
前三种还凑合,第四个匹配就不忍心看了,请大家帮忙丰富这个函数,使之适应,可以适当修改语句
大概明白了
何必呢,你确定你设置的这些所谓的规则使用者很好懂么?
你是想做一个类似自动阅卷的功能吧,我猜设置答案的老师会被搞晕菜的
做个事例便于理解
function pregRule($str){ $str = trim($str); return '/^'.str_replace(array(') ','[',']','//','/',' '),array(')?\s*','(','){1}','|','|','\s+'),$str).'$/'; }$str = "(a) b";echo pregRule($str).'<br>';$str = "[a//b]"; #加个中括号,方便转换echo pregRule($str).'<br>';$str = "(a/b) c";echo pregRule($str).'<br>';$str = "((it [is/’s]) the/a) cat // (It [is/’s]) (the) animal";echo pregRule($str).'<br>';
#result:
/^(a)?\s*b$/
/^(a|b){1}$/
/^(a|b)?\s*c$/
/^((it\s+(is|’s){1})?\s*the|a)?\s*cat\s+|\s+(It\s+(is|’s){1})?\s*(the)?\s*animal$/
前三种还凑合,第四个匹配就不忍心看了,请大家帮忙丰富这个函数,使之适应,可以适当修改语句
第四个主要的问题在于把“/”换成“|”
a/b
ca/bd
第一种情况下不需要考虑太多 直接把“/”换成“|”就ok
第二种情况下,就有要考虑的了。你是想得到cad,cbd还是想得到ca,bd呢?所以这个时候就不能只考虑“/”换成“|”
下面是你的第四个替换之后的表达式
/^( (it\s+(is|’s){1})?\s*the|a)?\s*cat\s+|\s+(It\s+(is|’s){1})?\s*(the)?\s*animal$/
只看前半部分
( (it\s+(is|’s){1})?\s*the|a)?\s*cat\s+ 中的
(it\s+(is|’s){1})?\s*the|a)
你想得到it\s+(is|’s){1})?\s*匹配的字符然后拼接the或者a
但实际上表达式呈现出来的是你想得到it\s+(is|’s){1})?\s*the或者a
你想要 i want(the|a),但实际上你却说 i want the|a

tomakephpapplicationsfaster,关注台词:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

到ImprovephPapplicationspeed,关注台词:1)启用opcodeCachingwithapCutoredUcescriptexecutiontime.2)实现databasequerycachingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandredececonnection.4 limitsclection.4.4

依赖注入(DI)通过显式传递依赖关系,显着提升了PHP代码的可测试性。 1)DI解耦类与具体实现,使测试和维护更灵活。 2)三种类型中,构造函数注入明确表达依赖,保持状态一致。 3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

databasequeryOptimizationinphpinvolVolVOLVESEVERSEVERSTRATEMIESOENHANCEPERANCE.1)SELECTONLYNLYNESSERSAYCOLUMNSTORMONTOUMTOUNSOUDSATATATATATATATATATATRANSFER.3)

phpisusedforsenderemailsduetoitsbuilt-inmail()函数andsupportiveLibrariesLikePhpMailerandSwiftMailer.1)usethemail()functionforbasicemails,butithasimails.2)butithasimimitations.2)

PHP性能瓶颈可以通过以下步骤解决:1)使用Xdebug或Blackfire进行性能分析,找出问题所在;2)优化数据库查询并使用缓存,如APCu;3)使用array_filter等高效函数优化数组操作;4)配置OPcache进行字节码缓存;5)优化前端,如减少HTTP请求和优化图片;6)持续监控和优化性能。通过这些方法,可以显着提升PHP应用的性能。

依赖性注射(DI)InphpisadesignPatternthatManages和ReducesClassDeptions,增强量产生性,可验证性和Maintainability.itallowspasspassingDepentenciesLikEdenceSeconnectionSeconnectionStoclasseconnectionStoclasseSasasasasareTers,interitationApertatingAeseritatingEaseTestingEasingEaseTeStingEasingAndScalability。

cachingimprovesphpermenceByStorcyResultSofComputationsorqucrouctationsorquctationsorquickretrieval,reducingServerLoadAndenHancingResponsetimes.feftectivestrategiesinclude:1)opcodecaching,whereStoresCompiledSinmememorytssinmemorytoskipcompliation; 2)datacaching datacachingsingMemccachingmcachingmcachings


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。