Re: Google Style Search Results

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Thats ok, sorry to have come across a bit 'narky' in that response by the way


I was just under some pressure at the time, I should really compose myself more before trying to lend a hand or some advice and head to the deep end in my ramblings..

haha


There really isn't much to Regex to be honest, whilst some patterns can look awfully complicated, and I will admit, generally you can't really neaten up long patterns as it just looks unreadable after wards..

For example:

  // Parse input string via forum URL substitution method..
  $parsedBodyStr = preg_replace("/\[URL(?:\=*([^\]]*))\]([^\[]*)\[\/URL\]/sie","'<A HREF=\"'.forumFormatPostURL(\"$2\").'\" TARGET=_BLANK>'.(strlen(\"$1\") > 0 ? \"$1\" : \"$2\").'</A>'",$parsedBodyStr);
  




It is simply a line that is designed to convert custom forum embedded hyperlink anchors into functional HTML standard anchors..

Whilst somewhat ugly to look at, it works 100% everytime. see my forum, like many other similiar class PHP forums, only allows url's to be entered via some custom structure: [url]http://the-spectrum.org[/url]

and as a small feature I also add the "title" parameter, [url=The Religion of Gurus!]http://php.net[/url]

These would be respectively entered in the code when someone views that forum page and its messages as:
http://the-spectrum.org
and The Religion of Gurus!



Now, trying to neaten that:

  // Parse input string via forum URL substitution method..
$parsedBodyStr = preg_replace(
    "/\[URL(?:\=*([^\]]*))\]([^\[]*)\[\/URL\]/sie",
    "'<A HREF=\"'.forumFormatPostURL(\"$2\").'\" TARGET=_BLANK>'.(strlen(\"$1\") > 0 ? \"$1\" : \"$2\").'</A>'",
    $parsedBodyStr
    );
  

Not much of an improvement.. Unfortunately you can't really go about entering spaces and so forth in your patterns otherwise it will render them unfunctional. You can go around it different ways..


I neaten my code and enhance reusability by simply conquering a particular pattern, and then that is just placed in a global $SYSTEM["REGEX_FILTER"] array which I can access later on in a shorthand version, keeps code legible

Particular if you are going to be using either a lot of patterns or even using one single pattern very often, Remember this does nothing for perfomance, purely asthetics only..


The easiest way to learn, just as I learnt mastering php, mysql, apache2 and picking up unix/bsd, c++, object pascal, and the list goes on..


Is to teach yourself.. I never paid a cent to learn php/mysql, never went to a single tutor/lecture/seminar etc..

Just hop on the net, you'll find tonnes of resources if you want examples, or better yet, invent a problem you want to solve with some kind of manipulation with a string, and try to code a pattern for it..


I guarantee you there will be a pattern for virtually any problem you could imagine needing a fix for..


I have never encountered a problem yet where a regex pattern can't pull me out of trouble 


Believe this or not, but I learnt the basics of regex patterns in Perl, but it wasn't till I hit the PHP PCRE [Perl-Compliant Reg Exp] references which refer to the pattern modifiers and syntax that I really picked up on it.. That syntax guide along with the modifier page are excellent to start working off.. Very specific, short examples given for many different aspects.. I personally love it anyway..



Well, enjoy.. ta ta for now!


---oOo--- Allowing users to execute CGI scripts in any directory should only be considered if: ... a.. You have no users, and nobody ever visits your server. ... Extracted Quote: Security Tips - Apache HTTP Server ---oOo--- ------oOo---------------oOo------ Julien Bonastre [The_RadiX] The-Spectrum Network CEO ABN: 64 235 749 494 julien@xxxxxxxxxxxxxxxx www.the-spectrum.org ------oOo---------------oOo------ 
----- Original Message ----- 
From: "Graham Cossey" <graham.cossey@xxxxxxxxx>
To: "Julien Bonastre" <julien@xxxxxxxxxxxxxxxx>
Cc: <php-db@xxxxxxxxxxxxx>
Sent: Saturday, December 10, 2005 12:42 AM
Subject: Re:  Google Style Search Results


On 12/9/05, Julien Bonastre <julien@xxxxxxxxxxxxxxxx> wrote:
> Yay, Questionnaire time
>
> I love this part of the game
>
>
>
> a) I'm not very familiar with regex at all and was wondering if you could
> tell me how your regex would handle two matched search strings that
> exist within a few words of each other in the text. For example "A
> larger server would be sufficient I think".
>
> Answer: As I think I may have mentioned "All I did was conjure up a regular
> expression "
>
> True, I didn't imply I wrote that regex in under 32 seconds...But I'm sure I
> was trying to come across with the fact that with improvement it could be
> much more powerful.
>
> Like this example:
>
> http://aries.the-spectrum.org/webdev/wawd/forums/search.php?q=were%2Badd&st=post&sb%5B%5D=*&maxres=25&ob=datetime&ot=DESC
>
>
> Is that better??
>
> multiple words within the same piece of string..
>
> Again, much much much more work can be done, this was a very quick "stub"
> example to show the flexibility of regex..
>
> Now a new issue that has been presented is if you DO have multiple words
> close together it will only grab x amount of words to the before and after
> that central word, including perhaps another keyword.. as you can see on
> above link..
>
>
> Again, give me another 3 minutes in the code and I'm sure I'll work that one
> too..
>
> b)Also in the link you provided (reproduced below) the first matched
> word is surrounded by 4 words and the second by 5 words, is there a
> reason for this?
>
> Answer: Ooh this is my favourite :-) Yes, great reason why when you conduct
> a search such as this:
> http://aries.the-spectrum.org/webdev/wawd/forums/search.php?q=sufficient%2Blarge&st=post&sb%5B%5D=*&maxres=25&ob=datetime&ot=DESC
>
> You received the 4 word and 5 word output.. Why don't you head over to the
> link that is generated on that search result entry??
>
> Look around at the actual content of that forum entry and you will soon see
> that the first match occurs on a line that physically only has 9 words,
> therefore it can only really match what exists.
>
> Good point though, for a split second I actually thought to myself there
> might be something wrong, but as usual and until I'm proved wrong; I'm right
> again. PHP and REGEX have never failed me.
>
> I'm sure you all are well aware already of the saying that describes how
> there is no such thing as computer errors, only stupid humans.
>
> And that is precisely it, I have been and still am a stupid human, and I
> will usually sit there for quite a while reloading and running a regex in my
> head to ensure it runs and parses as it should.
>
> Simple ones like this don't take too much planning, but they can get hairy
> :-)
>
>
>
>
> Hopefully that answers your queries Graham..
>
>
>
> Kindest Regards to everybody!
>
> Julien Bonastre
>
<snip>

Thanks for that Julien, I really must find the time to look more into Regexs.

--
Graham

[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux