草庐IT

php - php 中的正则表达式 : take the shortest match

我正在尝试做一个PHP正则表达式,但找不到正确的方法...假设我有这个字符串:“你好,我的{{nameisPeter}}and{{Iwanttoeatchocolate}}”我想取{{和}之间的部分但是如果我使用preg_match("/\{\{(.*)?\}\}/",$string)它只返回一个字符串“{{nameisPeter}}and{{Iwanttoeatchocolate}}”我怎么知道取}}的第一个巧合?谢谢 最佳答案 使用"/{{(.*?)}}/"表达式".*"是贪心的,尽可能多地获取字符。如果您使用".*?"会使用尽

php - preg_match 多个单词

我想测试一个字符串,看看它是否包含某些单词。即:$string="Theraininspainiscertainasthedryontheplainisoveranditisnotclear";preg_match('`\brain\b`',$string);但是那个方法只能匹配一个词。如何检查多个单词? 最佳答案 类似于:preg_match_all('#\b(rain|dry|clear)\b#',$string,$matches); 关于php-preg_match多个单词,我们在

PHP preg_match 只匹配数字和字母,没有特殊字符

我不想要preg_match_all...因为表单字段只允许数字和字母...只是想知道正确的语法是什么...没什么特别的……只需要知道只查找数字和字母的preg_match语句的正确语法。有点像preg_match('/^([^.]+)\.([^.]+)\.com$/',$unit)但这也不会寻找数字...... 最佳答案 如果你只想确保一个字符串只包含字母数字字符。A-Z、a-z、0-9不需要使用正则表达式。使用ctype_alnum()文档中的示例:上面的例子会输出:ThestringAbCd1zyZ9consistsofall

php - preg_match 特殊字符

如何使用preg_match查看是否有特殊字符[^'£$%^&*()}{@:'#~?>,;@|\-=-_+-¬`]存在于字符串中? 最佳答案 [\W]+将匹配任何非单词字符。但要仅匹配问题中的字符,请使用:$string="sadw$"if(preg_match("/[\[^\'£$%^&*()}{@:\'#~?>,;@\|\\\-=\-_+\-¬\`\]]/",$string)){//thisstringcontainatleastoneofthese[^'£$%^&*()}{@:'#~?>,;@|\-=-_+-¬`]charac

php - preg_match : check birthday format (dd/mm/yyyy)

如何创建检查生日输入以匹配像这样的dd/mm/yyyy格式的表达式?以下是我到目前为止得出的结果,但如果我输入99/99/9999,它也会用这个!if(!preg_match("/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/",$cnt_birthday)){$error=true;echo'';}我怎样才能确保dd只有01到31,mm只有01到12?但我确定如何限制yyyy...我认为理论上的9999应该是可以接受的...如果您有更好的主意,请告诉我!谢谢,刘 最佳答案 基于Tim的checkdate基于解决方案:使

php - 为允许 (!@#$%) 的密码验证创建 preg_match

我想创建一个preg_match函数来验证我的密码,但我不确定如何编写它以允许使用以下特殊字符:!@#$%.if(!preg_match(?????)$/',$password))这是我想在正则表达式中使用的密码规则:可以包含字母和数字必须至少包含1个数字和1个字母可能包含以下任何字符:!@#$%必须是8-12个字符感谢您提供的任何帮助。 最佳答案 我认为这应该是这样的:if(!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{8,12}$/',$password)){echo

php - 我怎样才能只从 preg_match 中获取命名的捕获?

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:howtoforcepreg_matchpreg_match_alltoreturnonlynamedpartsofregexexpression我有这个片段:$string='Hello,mynameisLinda.IlikePepsi.';$regex='/nameis(?[^.]+)\..*?like(?[^.]+)/';preg_match($regex,$string,$matches);print_r($matches);这打印:Array([0]=>nameisLinda.IlikePepsi[

php - Ruby 中的 preg_match_all 和 preg_replace

我正在从php过渡到ruby​​,我试图找出ruby​​中php命令preg_match_all和preg_replace的同源词。非常感谢! 最佳答案 preg_match_all在Ruby中的等价物是String#scan,像这样:在PHP中:$result=preg_match_all('/some(regex)here/i',$str,$matches);在Ruby中:result=str.scan(/some(regex)here/i)result现在包含一组匹配项。Ruby中preg_replace的等价物是String

Android studio 检索项目 : No resource found that matches the given name 'Theme.AppCompat.Light.DarkActionBar' 的父项时出错

我是androidstudio的初学者,我已经开始创建名为HelloWorld的应用程序,但始终在gradle构建选项卡中,它显示2个错误。我面临的错误是:Error:(1)Errorretrievingparentforitem:Noresourcefoundthatmatchesthegivenname'Theme.AppCompat.Light.DarkActionBar'.Error:Executionfailedfortask':app:processDebugResources'.>com.android.ide.common.process.ProcessExceptio

sql - 哪个 SQL 查询更好,MATCH AGAINST 还是 LIKE?

要在数据库中搜索“foo_desc”和“bar_desc”列中同时包含关键字“foo”和“bar”的行,我会执行以下操作:SELECT*FROMt1WHEREMATCH(t1.foo_desc,t2.bar_desc)AGAINST('+foo*+bar*'INBOOLEANMODE)或SELECT*FROMt1WHERE(CONCAT(t1.foo_desc,t2.bar_desc)LIKE'%foo%')AND(CONCAT(t1.foo_desc,t2.bar_desc)LIKE'%bar%')我预计最后一个查询的缺点是性能。好处是LIKE查询会找到MATCHAGAINST找不到