1

I've been trying to make a few of functions based on RegEx and most of them use \Q and \E as some of the RegEx pattern is user input.

So, let's say hypothetically that we're using the delimiter / and want to match it against / the function would construct something amongst the lines of /\Q/\E/.

I'm not sure why /\Q/\E/ doesn't match / but with every other delimiter it does, unless you use the same delimiter as input.

Maybe, it considers the delimiter the end, even though, it's in a literal-only block and the escape as literal. Not sure, tried a bunch.

Hopefully someone can push me into the right direction as to what workarounds there are for this issue.

KaekeaSchmear
  • 1,548
  • 5
  • 18
  • 30
  • Is there anything keeping you from using another delimiter or just escape the `/`, without using `\Q` and `\E` – Sebastian Proske Jan 07 '17 at 21:49
  • @SebastianProske Using a different delimiter will move the issue and not solve it, the user input could also be whatever other delimiter I might use. Also, not using `\Q` and `\E` is a security issue in this case. TL:DR - Yes, a few things in fact. – KaekeaSchmear Jan 07 '17 at 22:07
  • @SebastianProske Err, that was dense. I thought preg_quote only backslashed the quote. Sorry & Thanks! – KaekeaSchmear Jan 07 '17 at 22:53

1 Answers1

1

It helps to understand that / is not a regex metacharacter, like * or (. It's special because you're using it to delimit the regex itself, and the only way to escape the regex delimiter is with a backslash (\/).

But you shouldn't need to use \Q and \E. The preg_quote() method takes a delimiter argument, so it correctly adds backslashes everywhere they're needed.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
  • I'm aware of that. However, I really do need to use `\Q` and `\E` since I don't want `regex` characters to be interpreted as such. All the input should be literal. – KaekeaSchmear Jan 07 '17 at 22:10
  • @Removed So how do you handle a `\E` inserted by the user? Have you read what `preg_quote` does? – Sebastian Proske Jan 07 '17 at 22:14
  • @SebastianProske Updated post with an illustration as to `why` – KaekeaSchmear Jan 07 '17 at 22:21
  • @SebastianProske Yes, I'm fully aware of what `preg_quote` does as I stated already preciously. It wouldn't work in this case because you'd get: `/\Q\/\E/` which doesn't match `/`. – KaekeaSchmear Jan 07 '17 at 22:32
  • @Removed get rid of `\Q` and `\E` and just use `preg_quote`, as stated in this answer. – Sebastian Proske Jan 07 '17 at 22:35
  • @SebastianProske Yes, and then, as in my illustration, entire `$start` and `$end` will be interpreted as RegEx, ruining the entire point of writing a function like that. Trust me, this answer is not even close to good. – KaekeaSchmear Jan 07 '17 at 22:40
  • @SebastianProske A better answer is to go with my current ghetto fix which is to ban the delimiter from user input entirely so I can still use `\Q` and `\E` for the rest, but this seems unnecessarily ugly. – KaekeaSchmear Jan 07 '17 at 22:43
  • @SebastianProske Do you even know what `\Q` and `\E` is? – KaekeaSchmear Jan 07 '17 at 22:44
  • Yep, I'm just too dump to understand your problem. http://ideone.com/AfsI0O – Sebastian Proske Jan 07 '17 at 22:47