0

The text is like this:

$string = "  <L UXUHSXUS>importantText</L>";

I would like to use the lookbehind feature in PHP Regular Expression but it generates an error everytime I try to put .+ or any regular expression things

preg_match_all('%(?<=<L.+>)([\s\S]*?)(?=</LF>)%',$string, $matches);

this gives an error.

what should I do to add .+ or .* or any reserved regular expression things in lookbehind and lookahead?

Grego
  • 2,220
  • 9
  • 43
  • 64
  • Are you trying to [parse HTML with regexes](http://stackoverflow.com/questions/1732348/)? At the end of that path only madness awaits. – outis Dec 10 '11 at 11:23
  • 1
    yeaa I know its not the ideal, its much better to use some good parser out there, but since its predictable what is coming (therefore its not from regular users) its alright. – Grego Dec 10 '11 at 12:32

2 Answers2

2

Since you only use positive lookarounds you could probably replace all lookarounds with non-capturing groups (?:) instead:

(?:<L.+>)([\s\S]*?)(?:</LF>)

Note that you might want to make the first search non-greedy:

(?:<L.+?>)([\s\S]*?)(?:</LF>)
Marcus
  • 12,296
  • 5
  • 48
  • 66
  • Perfect that's exactly what I was looking for, but just for curiosity what is the difference of this one and positive/negative lookarounds? thanks! – Grego Dec 10 '11 at 12:45
1

You can't. Lookbehinds don't support full regexes. The pattern(s) must have a fixed length, though if you're using alternate patterns (with |), each alternate doesn't have to have the same length as the others:

/(?<=foo|quux|foobar)duck/
outis
  • 75,655
  • 22
  • 151
  • 221