J-H
J-H

Reputation: 1869

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:

^{\$.*}$

Upvotes: 1

Views: 191

Answers (1)

Ibrahim Najjar
Ibrahim Najjar

Reputation: 19423

Try the following regex:

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

Regex101 Demo

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 }.

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.

Upvotes: 3

Related Questions