Points of Required Attention™
Please chime in on a proposed restructuring of the ROM hacking sections.
Views: 88,542,912
Main | FAQ | Uploader | IRC chat | Radio | Memberlist | Active users | Latest posts | Calendar | Stats | Online users | Search 05-05-24 11:48 PM
Guest: Register | Login

0 users currently in Computing | 1 guest

Main - Computing - Quick regex problem New thread | New reply


Kailieann
Posted on 03-24-07 03:56 PM Link | Quote | ID: 18693


Red Cheep-cheep
Level: 34

Posts: 136/217
EXP: 253428
Next: 223

Since: 02-19-07
From: Beyond

Last post: 5646 days
Last view: 5646 days
Okay. My regex skills are a little rusty, so forgive me if I'm overlooking something obvious here.

What I'm trying to do is create a regex pattern that will match text outside square brackets -- specifically, this is for PHP's preg_replace() function.

The pattern I have so far is (^|\])(.*?)?(\[|$)
The problem I'm having is that this pattern is matching the brackets as well as the text. Which, with that particular pattern, means it's working properly.

What I need to know now is how do I make it so the brackets are required in order to make a match, but aren't returned as part of the match.

Any help would be appreciated.

Csaw
Posted on 03-25-07 06:08 AM Link | Quote | ID: 18951

Newcomer
Banned for trolling until 7/12.
Level: 3

Posts: 1/1
EXP: 79
Next: 49

Since: 03-25-07

Last post: 6251 days
Last view: 6251 days

// What you want to replace
$replacements = array('[', ']');

// str_replace returns $string with [ and ] replaced by blanks, this is
// then the string that is returned by preg_replace when $pattern is matched
// within $string
preg_replace($pattern, str_replace($replacements, '', $string), $string);

Main - Computing - Quick regex problem New thread | New reply

Acmlmboard 2.1+4δ (2023-01-15)
© 2005-2023 Acmlm, blackhole89, Xkeeper et al.

Page rendered in 0.018 seconds. (321KB of memory used)
MySQL - queries: 42, rows: 52/53, time: 0.015 seconds.