regex - Smarty 正则表达式匹配

标签 regex string smarty match

我有一个 smarty 变量,我想知道它是否与某些字符串匹配

"<whatever>_thestring_<whatever>" 

哪里<whatever>代表任何字符序列(或无字符)。

是否有某种方法可以针对 *_thestring_* 之类的内容进行测试?

最佳答案

使用 smarty 检查一个字符串是否存在于另一个字符串中:

{assign "haystack1" "whatever_thestring_whatever"}
{assign "haystack2" "whatever_thestrings_whatever"}

Test haystack1 {if $haystack1|strstr:"_thestring_"}Found!{/if}<br />
Test haystack2 {if $haystack2|strstr:"_thestring_"}Found!{/if}<br /><br />

输出:

Test haystack1 Found!
Test haystack2

或者您可以使用 smarty 中的正则表达式进行更复杂的搜索:

{assign "haystack1" "whatever_thestring_whatever"}
{assign "haystack2" "whatever_thestrings_whatever"}

{assign "check_haystack1" $haystack1|regex_replace:"/_thestring_/":" "}
{assign "check_haystack2" $haystack2|regex_replace:"/_thestring_/":" "}

Test haystack1  {if $check_haystack1 !== $haystack1}Found!{/if}<br />
Test haystack2  {if $check_haystack2 !== $haystack2}Found!{/if}<br />

其输出:

Test haystack1 Found!
Test haystack2 

关于regex - Smarty 正则表达式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18125918/

相关文章:

C程序,字符串到int数组命令行排序

regex - R中的重叠匹配

regex - 在 perl 中处理 curl 输出

string - 在字符串上的when表达式中设置忽略大小写值

database - 如何从搜索查询中搜索子项?

php - 将树状对象结构转换为数组并交互整个事物

php - 在类别模块中显示父类别名称

php - 聪明的当前语言

javascript - 如何从正则表达式匹配函数中获取匹配的组?

regex - 在 linux 中搜索文件