Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
-
Grim...
- DevNet Resident
- Posts: 1445
- Joined: Tue May 18, 2004 5:32 am
- Location: London, UK
Post
by Grim... »
Hello, I need regex help again
I want to replace anything that's inside single quotes, ignoring escaped quotes.
To clarify:
would become
Code: Select all
this is a 'something else' hot day
and
would becode
I've got
Code: Select all
$q = preg_replace("/'.*?'/s", "'something else'", $q);
but now I'm stuck

-
Grim...
- DevNet Resident
- Posts: 1445
- Joined: Tue May 18, 2004 5:32 am
- Location: London, UK
Post
by Grim... »
Oh, and it's got to be non-greedy too, so if I have
I need
Code: Select all
I 'something else' I can 'something else' it
-
feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Post
by feyd »
Look through my posts in the regex board. I've posted code that supports escaped quotes multiple times.
-
stereofrog
- Forum Contributor
- Posts: 386
- Joined: Mon Dec 04, 2006 6:10 am
Post
by stereofrog »
Grim... wrote:Hello, I need regex help again
I want to replace anything that's inside single quotes, ignoring escaped quotes.
How about
Code: Select all
$regexp = "/
' # open quote
(
(\\\\ .) # slash + character
| # or
. # just a character
)* # many times
' # close quote
/x";
@feyd, didn't find your post(s), care to post the link or your expr?
-
feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Post
by feyd »
One of the more detailed ones:
viewtopic.php?t=65940
You even posted in that one stereofrog.

-
GeertDD
- Forum Contributor
- Posts: 274
- Joined: Sun Oct 22, 2006 1:47 am
- Location: Belgium
Post
by GeertDD »
-
stereofrog
- Forum Contributor
- Posts: 386
- Joined: Mon Dec 04, 2006 6:10 am
Post
by stereofrog »
Ah, thanks!
Not a valid regexp in php (think of slash doubling)
-
GeertDD
- Forum Contributor
- Posts: 274
- Joined: Sun Oct 22, 2006 1:47 am
- Location: Belgium
Post
by GeertDD »
This is what it should look like in PHP:
Code: Select all
preg_match_all('/\'((?:[^\'\\\]|\\\.)*)\'/s', "I 'think' I 'can\'t'.", $m);