SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
Thread: Need help with regex
-
May 24, 2007, 09:53 #1
- Join Date
- Oct 2004
- Posts
- 45
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Need help with regex
Supposed I have a string like this:
Code:bla bla {strong} bla bla bla {bold} bla bla
Code:/\{.*\}/
Code:{strong} bla bla bla {bold}
-
May 24, 2007, 10:05 #2
- Join Date
- Jul 2006
- Location
- Kansas City, MO
- Posts
- 280
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
/\{.*?\}/
-
May 24, 2007, 19:35 #3
- Join Date
- Oct 2004
- Posts
- 45
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ah okay, thanks... lol, why I missed that one
actually I'm working for something like:
Code:bla bla {{strong} bla} bla bla {bold} bla bla
Code:{{strong} bla} {strong} {bold}
-
May 25, 2007, 02:50 #4
- Join Date
- Apr 2004
- Location
- germany
- Posts
- 4,324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Simple iteration would be enough
PHP Code:$s = 'bla bla {{stro{ng}} bla} bla bla {b{o}ld}bla bla';
$re = '/\{([^{}]*)\}/e';
while(preg_match($re, $s))
$s = preg_replace($re, '$matches[]="$1"', $s);
print_r($matches);
-
May 25, 2007, 05:44 #5
- Join Date
- Oct 2004
- Posts
- 45
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
That's cool simple solution stereofrog!
Darn... I'm too late seeing your reply here, I have already made a recursive function, it's more complicated lol.
Btw, I want to have '\{' and '\}' not being translated, how to tell regex to ignore that?
For example:
Code:bla \{bla\} {{strong} bla} bla bla {bold} bla bla
thanks.
-
May 25, 2007, 06:06 #6
- Join Date
- Apr 2004
- Location
- germany
- Posts
- 4,324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
To handle escapes, change bounding \{'s to (?<!\\\\)\{ , inner group should be something like ((\\\\.|[^{}])*).
-
May 25, 2007, 07:42 #7
- Join Date
- Oct 2004
- Posts
- 45
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ok thanks, will try it..
Bookmarks