views:

58

answers:

2

What would the regular expression be for removing any content between a quote, and the directory "uploads/"?

Using a regexpression generator, I get this: (?<=\=")[^]+?(?=uploads/)

$block_data = preg_replace('/(?<=\=")[^]+?(?=uploads/)/g','',$block_data);

But seems to be removing everything :(

+2  A: 

You should escape the "/" in "uploads/" and g isn't a valid modifier, plus [^] is invalid, I guess you wanted . instead.

Here is your regex :

/(?<=\=").+?(?=uploads\/)/

The test on ideone

Colin Hebert
+1  A: 

The simple solution would be

$block_data = preg_replace('/(?<=").*?(?=uploads\/)/','',$block_data);

Changes made:

  1. Simplified your lookbehind and lookahead assertions
  2. escaped the / in the lookahead
  3. removed the g modifier, which is unnecessary in PHP

This works, so far as I can tell, reducing first"middle/uploads/end" to first"uploads/end".

lonesomeday
By simplifying you remove the "=" part in the look-behind (which could be important) and you removed the laziness (which could be dangerous) and replaced it by an optional `.` (which is wrong given the initial regex).
Colin Hebert
@Colin the spec is "after a quote" (hence the lookbehind change) and "any content" (hence the change to `.*`). I had already changed the greedy `*`.
lonesomeday