[Regex] How do I find values in brackets...

Associate
Joined
2 Apr 2004
Posts
674
Location
Melbourne
I am currently learning regex and its making my mind hurt a bit.

Ideally, I wish I could figure it out, but maybe one of you guys could help me.

How would I go about matching the following pattern and pulling out the value between the brackets and replacing the whole lot.

So say I have the following

Code:
{value}

and I want to replace it with

Code:
cheese

how would I do it?
 
Sic said:
Code:
{[a-zA-Z0-9]+}
will match words and numbers, nothing else
To be pedantic, letters and numbers (words implies more than one word, which in turn implies spaces [not caught by the expression]).
 
sorry, you're right. I know what I meant, but it wasn't what I said! That should still work for what the OP's after though
 
yes it does work and it works great.

its for a simple template engine I'm writing and have used str_replace to replace the values I do know and that regex to replace all unset values.

so thanks everyone :)
 
You may not want to implement this.. what if the users content contains "Blah blah {foobar} blah blah" you'll end up replacing it..
 
ereg_replace ("{\w}", "cheese", userText)

The following should do what you want. I'm not that great with PHP, but it looks as if that is the regex replace function. \w is short hand for a-zA-Z0-9.
 
Back
Top Bottom