mark.d92 Posted July 12, 2011 Share Posted July 12, 2011 Hi, I am having an issue understanding how these patterns work. Please can someone provide me the search term for the following issue. I need to find <base href="?????"> and replace with this <base href="$home"> Thanks for your help. Mark Quote Link to comment Share on other sites More sharing options...
djlee Posted July 12, 2011 Share Posted July 12, 2011 A quick and dirty regex like /<base\s*href=[\"\'](.*?)[\"\']>/is will match the contents of href so you can replace it. " .* " is a greedy expression that basically means select everything possible. " ? " defines as few as possible (so it will match only up to the first quote is finds, need this otherwise it wil match up to the last quote which could be 1000 lines down the script such as many strings of "> exist usually) Get yourself a copy of expresso, it did me wonders learning regex with it. Quote Link to comment Share on other sites More sharing options...
mark.d92 Posted July 13, 2011 Author Share Posted July 13, 2011 Thanks a lot. is this expresso app for mac? and do you know if its possible to find out what the wildcard that was found is? Thanks, Mark Quote Link to comment Share on other sites More sharing options...
djlee Posted July 13, 2011 Share Posted July 13, 2011 its for windows, you could run it under wine, its not as if you need it constantly. If you dont like wine you can probably find a mac regex builder, but i tried loads of builders for windows and i found expresso to be the best one. Personally preference i guess. What do you mean by wildcard ? The * just means match any, so it will match href "" as well as href="really long string" . You could have used ".+?" and that would have meant "one or more" but if the href was ever empty, it wouldnt match anything. I guess it depends on what your actually trying to accomplish. If by "find out what wildcard matched" you mean return the contents of href, then preg_match would do that. Not sure if theres a return matches in preg_replace, you'd have to check the php docs, its something ive never required to do so im not sure if theres a flag in the function for this or not Hopefully that makes some sort of sense Quote Link to comment Share on other sites More sharing options...
mark.d92 Posted July 13, 2011 Author Share Posted July 13, 2011 Thanks for your point in the right direction. I have used the expresso software and managed to get (\<base\s*href\=(?:\"|\')(?:.*?)(?:\"|\')\s*/\>) which does the job really well. Thanks again 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.