jamesmiller Posted September 8, 2011 Share Posted September 8, 2011 Hey guys, basically i need a regex to reterieve part of a string but exclude strings that end with a certain variable. I was wondering how this could be achieved so far i have: preg_match('!/load/!is',$href) but it retrieves strings that have /load/files/wjnjsnws/ which i dont want i just want the /load/ thank you guys Quote Link to comment Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 Give us some sample data, otherwise we're just shooting in the dark. My first attempt as a blind shot: %/load/$%i Keep in mind, the 's' modifier is pointless if you don't use a full stop. Quote Link to comment Share on other sites More sharing options...
jamesmiller Posted September 8, 2011 Author Share Posted September 8, 2011 ok the content is <a href="/load/gg/4358813f031ffa01ba1164c238696eb8e24079b24481/files/gregr" rel="nofollow" onclick="return listfiles(this,750,50,'2px solid',0,0,'img4358813f031ffa01ba1164c238696eb8e24079b24481');"> <img name="img4358813f031ffa01ba1164c238696eb8e24079b24481" src="/images/expand.gif" alt="File Listing" border="0"></a> <a href="/load/gg/4358813f031ffa01ba1164c238696eb8e24079b24481" class="BlckUnd"><font color="#CC0000">gg</font> < i want to get one of these which is the /load/gg/4358813f031ffa01ba1164c238696eb8e24079b24481 thank you Quote Link to comment Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 Something like this? %(/load/gg/(?:[a-z0-9]++))%i Or, if you'd like to be more generic, %(/load/gg/(?:[^/"]++))%i Quote Link to comment Share on other sites More sharing options...
jamesmiller Posted September 8, 2011 Author Share Posted September 8, 2011 that works but its still matching false positives i want it to not match anything that is /load/gg/4358813f031ffa01ba1164c238696eb8e24079b24481/files/etc and match everything with /load/gg/4358813f031ffa01ba1164c238696eb8e24079b24481. Thank you Quote Link to comment Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 There is no way that the RegEx i provided would ever match '/load/gg/4358813f031ffa01ba1164c238696eb8e24079b24481/files/etc' in it's entirety. It will, however, extract '/load/gg/4358813f031ffa01ba1164c238696eb8e24079b24481' out of '/load/gg/4358813f031ffa01ba1164c238696eb8e24079b24481/files/etc'. Is this not a good solution? Perhaps you should give me detailed examples where my expression returns false positive. Try this. I can only guarantee it will work with the sample data you've provided. %(/load/gg/(?:[^/"]++))"%i Quote Link to comment Share on other sites More sharing options...
jamesmiller Posted September 8, 2011 Author Share Posted September 8, 2011 thank you, but i want the regex to pick out only '/load/gg/4358813f031ffa01ba1164c238696eb8e24079b24481' so i need a regex that excludes the link wuth files/etc on the end ? Quote Link to comment Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 O RLY? This is going beyond my patience levels. <?php $string = <<<HEREDOC <a href="/load/gg/4358813f031ffa01ba1164c238696eb8e24079b24481/files/gregr" rel="nofollow" onclick="return listfiles(this,750,50,'2px solid',0,0,'img4358813f031ffa01ba1164c238696eb8e24079b24481');"> <img name="img4358813f031ffa01ba1164c238696eb8e24079b24481" src="/images/expand.gif" alt="File Listing" border="0"></a> <a href="/load/gg/4358813f031ffa01ba1164c238696eb8e24079b24481" class="BlckUnd"><font color="#CC0000">gg</font> <; HEREDOC; $expr = '%/load/gg/(?:[^/"]++)%i'; preg_match( $expr, $string, $match ); print_r( $match ); ?> Output Array ( [0] => /load/gg/4358813f031ffa01ba1164c238696eb8e24079b24481 ) Until you start APPLYING the help you're getting, I'm done with this thread. Quote Link to comment Share on other sites More sharing options...
jamesmiller Posted September 8, 2011 Author Share Posted September 8, 2011 Ok sorry bud, ive tried applying several times before i posted that, its not working because its still picking out /load/gg/4358813f031ffa01ba1164c238696eb8e24079b24481/files/gregr and still outputting that . Quote Link to comment Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 I don't understand how my snippet above is producing the correct results while yours isn't. Have you copied and pasted my code, and tried it on your system? Perhaps you have given us bad data to try and build a RegEx off of. A complete set of data may help with this. Again, though, the expression I provided should NEVER match '/load/gg/4358813f031ffa01ba1164c238696eb8e24079b24481/files/gregr' Quote Link to comment Share on other sites More sharing options...
jamesmiller Posted September 8, 2011 Author Share Posted September 8, 2011 Ok im not sure why it isnt working either bud, i think the /gg/ can be anything as it changes and so this is the manipulation i have got: $expr = '%/load/(.*?)/(?:[^/"]++)%i'; if( preg_match($expr,$href)) $hrefs[] = $href; // store href } Quote Link to comment Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 Next time, please tell me you're changing the expression What other string could 'gg' contain? Try this RegEx. %/load/[^/]++/[a-z0-9]++%i Quote Link to comment Share on other sites More sharing options...
jamesmiller Posted September 8, 2011 Author Share Posted September 8, 2011 Thank you, still returning them all, sorry about this . Quote Link to comment Share on other sites More sharing options...
jamesmiller Posted September 8, 2011 Author Share Posted September 8, 2011 and gg can contain anything Quote Link to comment Share on other sites More sharing options...
xyph Posted September 8, 2011 Share Posted September 8, 2011 Are you changing my RegEx around? Because as it is, it's impossible for the RegEx I posted to match /load/gg/4358813f031ffa01ba1164c238696eb8e24079b24481/files/gregr in it's entirety Quote Link to comment Share on other sites More sharing options...
jamesmiller Posted September 11, 2011 Author Share Posted September 11, 2011 Sorry was away for the weekend, no i havent changed it using exactly that expression and still returning all the values :-( . Quote Link to comment Share on other sites More sharing options...
xyph Posted September 11, 2011 Share Posted September 11, 2011 Then you're using a different sample set than what you've provided. Quote Link to comment Share on other sites More sharing options...
jamesmiller Posted September 11, 2011 Author Share Posted September 11, 2011 im really not , i dont understand why t wont work the code i have is $expr = '%/load/(.*?)/(?:[^/"]++)%i'; if( preg_match($expr,$href)) $hrefs[] = $href; // store href } and it wont work Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.