0

Trying to create a pattern that includes an or condition.

$pattern["body"]='/(<\/a><\/li><\/ul><\/div><p>|<h2>)(.*)<div class="like">/s';

The pattern should recognize any text between a series of "<\/a><\/li><\/ul><\/div><p>" or a "<h2>" as the beginning delimiter, but apparently the or condition does not work in this case. Anyone can offer a hint for the correct syntax!? I am striving for 1 hour and I have lost the focus and patience in this rule. THank you in advance.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
Al-Punk
  • 3,531
  • 6
  • 38
  • 56
  • 1
    You specified `ereg` in the question title. I should point out that the `ereg` functions in PHP have been deprecated and shouldn't be used any more. You should be using the `preg_` fuctions instead. See the PHP manual for more. – Spudley Oct 06 '11 at 20:42
  • Sorry! I ment regex. I will change the subject – Al-Punk Oct 06 '11 at 20:43
  • 1
    [NO REGEX HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – cwallenpoole Oct 06 '11 at 20:46

3 Answers3

1
"(?<=</a></li></ul></div>|<h2>).*"

see below grep test:

kent$  echo "</a></li></ul></div>something"|grep -Po "(?<=</a></li></ul></div>|<h2>).*"                                                  
something

kent$  echo "<h2>something"|grep -Po "(?<=</a></li></ul></div>|<h2>).*"                                                                  
something
Kent
  • 189,393
  • 32
  • 233
  • 301
0
'%(</a></li></ul></div><p>|<h2>)(.*)<div class="like">%s'

I don't have any sample data to test against but it looks like you were there. I only changed the delimiter so it wouldn't have to be escaped, I find it's easier to understand.

nachito
  • 6,975
  • 2
  • 25
  • 44
  • Thank you for the reply. For some unclear reason it does not work, but the example provided above from Kent works. – Al-Punk Oct 06 '11 at 21:11
  • @Armand - so, then the differences are the extra `

    ` and the trailing `

    –  Oct 06 '11 at 21:47
  • I added the extra

    and the

    – Al-Punk Oct 06 '11 at 21:54
  • 1
    If you added the extra's back, I would say your lucky that it apparently PHP (PCRE) allows alternation with its fixed length lookbehinds. As soon as there is a spurrious whitespace between tags it might fail. –  Oct 06 '11 at 23:33
0

This might work http://www.ideone.com/zYB1n
But, I'm no php expert.

$regex = '/(?:<\/a>\s*<\/li>\s*<\/ul>\s*<\/div>\s*<p>|<h2>)(?<Text>[\S\s]*)(?=<div class="like">)/';
if (preg_match( $regex, $str1, $matches ))
  print_r( $matches['Text'] );