简体   繁体   English

PHP搜索并替换正则表达式模式

[英]PHP search and replace regex pattern

I'm trying to get a string which matches by a regex pattern ( {$ ... } ). 我正在尝试获取由正则表达式模式({$ ...})匹配的字符串。 But I don't want the brackets and the $ sign returned. 但是我不希望括号和$符号返回。

For example 例如

{$Testpath}/Testlink

should return 应该回来

Testpath

My regex pattern looks like this at the moment: 目前,我的正则表达式模式如下所示:

^{\$.*}$

Try the following regex: 尝试以下正则表达式:

^\{\$\K[^}]*(?=\})

Regex101 Demo Regex101演示

This expression mathces start-of-string ^ then a literal { then a literal $ then it ignores those using \\K anchor, then it matches one or more characters which aren't a } then it looks ahead (?=\\}) for a literal } . 该表达式先计算字符串的开头^然后是文字{然后是文字$然后忽略使用\\K锚的字符,然后匹配一个或多个不是}字符,然后向前看(?=\\})文字}

You may not need the end-of-line anchor $ because the text you are trying to match might not end at the end of the string and you may not need the start-of-line ^ anchor for the opposite reason, that is the pattern you are trying to match may not be at the start of the string or line. 您可能不需要行末锚$因为您要匹配的文本可能不会在字符串的末尾结束,并且由于相反的原因,您可能不需要行起始^锚,即您尝试匹配的模式可能不在字符串或行的开头。

I think you should remove ^ and $ and use the global modifier. 我认为您应该删除^$并使用global修饰符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM