0

Here is my problem...

Based on the code below... I need a PHP script which uses Regex or some other way to extract the name of the div class which contains the text: TEXT1. In this case, return of the script should be: CLASSNAME_3

 <div class="CLASSNAME_1">
  <span class="CLASSNAME_2"></span>
   <div class="CLASSNAME_3">
   TEXT1
   </div>
 </div>

Reason for the script is, that the name of the class (CLASSNAME_3) is constantly changing, while the content of the class (TEXT1) is not.

I've researched this all over the web and also on stackoverflow, but I am unable to find the answer, nor any similar question which could help me to do this.

1 Answer 1

3

It could be possible through positive lookahead.

/\"([^"]*)(?=\">\s*TEXT1)/gm

It matches CLASSNAME_3 because it is followed by the string TEXT1.

DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for nice work on the regex, Avinash, but you have a typo in the explanation: your regex actually doesn't need DOTALL to work... There are no dots! :)
+1 your answer has cleared my doubts about lookahead, though @zx81 send me a link about this but still I had some doubts and they are clear now. Thanks to you too @_zx81 will also follow lookbehind concept. :)
@HarpreetSingh lookbehind and lookahead options play a major role in regex. If you have any doubts just get into regex chatroom and ask questions about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.