views:

10917

answers:

6

Hi, unfortunately despite having tried to learn regex at least one time a year for as many years as I can remember, I always forget as I use them so infrequently.This year my new years resolution is to not try and learn regex again - So this year to save me from tears I'll give it to stackoverflow. (Last Christmas remix).

So basically I want to pass in a string in this format "{getThis}", and be returned the string "getThis". Could anyone be of assistance in helping to stick to my new years resolution?

Regards,

Chris


Related (but not identical) questions on stackoverflow:

and probably others.

+21  A: 

Try

/{(.*?)}/

That means, match any character between { and }, but don't be greedy - match the shortest string which ends with } (the ? stops * being greedy). The parentheses let you extract the matched portion.

Another way would be

/{([^}]*)}/

This matches any character except a } char (another way of not being greedy)

Paul Dixon
Odd to get a downvote some 9 months later...
Paul Dixon
Yeah, someone downvoted mine too. (?)
Kev
Awesome, I was just looking for something like this! :)
Alex
added modifier q and was good to go. bedanke
Gutzofter
+13  A: 

If your string will always be of that format, a regex is overkill:

>>> var g='{getThis}';
>>> g.substring(1,g.length-1)
"getThis"
Kev
An example of excellent requirements analysis. The customer says "give me this solution" and you heard "give me a solution to this problem".
Darron
not to be nitpicky but shouldnt the code be g.substring(1,g.length-2)
almog.ori
Actually, it shouldn't. substring() is 0-based, whereas the length is 1-based, and substring()'s second argument is which position in the original string to stop at, rather than how long the string returned should be. Try it for yourself, and then try with 5 as the first argument. (The above I actually just copied and pasted from Firebug, to make sure it was initially correct.)
Kev
Substringing is one of those things that changes based on the language you work in. Javascript takes the index to stop at, PHP takes the length of the desired end result (unless it's negative, in which case it takes the number of characters to remove), C# is different again...nice and confusing.
jvenema
...and Python just has slicing, which IMO is better than anything else :p.
chpwn
Didn't the OP tag the question 'javascript'?
Kev
+5  A: 
/\{([^}]+)\}/

/        -    delimiter
\{      -    opening literal brace escaped because it is a special character used for quatifiers eg {2,3}
(        -    start capturing
[^}]  -    character class consisting of
    ^      -    not
    }       -    a closing brace (no escaping necessary because special characters in a character class are different)

+      -    one or more of the character class
)       -     end capturing
\}     -     the closing literal brace
/       -     delimiter
meouw
I was looking to find if curly brackets were special chars in java regexp and this helped alot. 10x.
Amir Arad
@meouw,thank you so much
Gutzofter
+1  A: 

This one works in Textmate and it matches everything in a CSS file between the curly brackets.

\{(\s*?.*?)*?\}

selector {. . matches here including white space. . .}

If you want to further be able to return the content, then wrap it all in one more set of parentheses like so:

\{((\s*?.*?)*?)\}

and you can access the contents via $1.

This also works for functions, but I haven't tested it with nested curly brackets.

Alex Kessaris
A: 

This was the question:

"So basically I want to pass in a string in this format "{getThis}", and be returned the string "getThis".

All of the examples above return {getThis}, not getThis. Excluding the opening and closing brackets is the tricky part!

If anyone can figure it out, well that would be nice.

howdy
The examples that return {getThis} out of "some text {getThis} some more text" are what should be done with regex. To actually get rid of the curlys it makes much more sense to use substring(1,str.length-1) instead of regex
Smalcat
A: 

If you're using PHP and you have the following strings and want to extract the albumid.

http://picasaweb.google.com/data/entry/base/user/user/albumid/5241920314542926625?alt=rss&hl=en_US
http://picasaweb.google.com/data/entry/base/user/user/albumid/5200211976033472801?alt=rss&hl=en_US

preg_match('/albumid\/(.*?)\?/',$item->guid, $match);

$match would contain something like this.

Array ( [0] => albumid/5241920314542926625? [1] => 5241920314542926625 )
Array ( [0] => albumid/5200211976033472801? [1] => 5200211976033472801 ) 

So basically use match[1] to get the value that you need inside your delimiter.

Herbert Balagtas