We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

New Plugin: Perl Regex

Development and Programming

odspi's avatar
odspi
29 posts
16 years ago
odspi's avatar odspi

This plugin is for people like me for whom working with text without the perl regex engine is like not having a thumb.

It is also my first ever EE plugin. It doesn’t do much, and I’m not sure whether its functionality isn’t fully covered elsewhere.

It provides an EE interface to preg_match() and preg_replace(). That’s all!

PARAMETERS:

1) regex - a Perl regular expression.

2) string - a string

3) string_replace - Optional. replacement string. If not supplied, plugin defaults to using preg_match()

4) limit - optional. Default is -1 which replaces everything. Sets maximum number of replacements.

5) max_parens - optional: creates additional conditional variables regex_n up to n=max_parens

6) debug - optional: a value of ‘y’ allows DEBUGGING variables

VARIABLES:

1) regex_1 regex_2 … regex_n : matching parenthesis ( only if string replace wasn’t provided ) 2) result_string : the result of the operation ( only if string_replace was provided ) 3) matches : boolean 4) not_matches: boolean 5) match_count: number of matches or replacements

DEBUG VARIABLES: 6) string –> the parameter string 7) pattern –> the parameter regex

EXAMPLE OF USAGE:

{exp:perl_regex regex="@(.+://)?(\\w+)\\.(\\w+)\\.(net|com|org|info|\\w{2})(/([^/]+/)+)([^/]+.\\w)$@"
                string="http://www.yesterday.br/today/the_day/tomorrow.html"
                debug="y" }

         {regex_1} is the service.

         {regex_2} is the subdomain. 

         {regex_3} is the domain name. 

         {regex_4} is the top domain. 

         {regex_5} is full path 

         {regex_6} is the last dir in the path

         {regex_7} is file name 


         matched # {match_count} times
         {string}
         {pattern}

{/exp:perl_regex}
{exp:perl_regex string="see a little goblin/see his little feet"
                regex="@goblin@"
                replace_string="elf"
                debug="y" }

     {if matches}
       YES! WE GOT A MATCH!
       <h3>{replace_result}</h3>
     {/if}
     {if not_matches}
       NO! CANT FIND ANYTHING

     {/if}
     matched # {match_count} times
     {string}
     {pattern}
     {replace_string}

{/exp:perl_regex}

======================

I needed it because I wanted to display a custom weblog field inside a category_archive (inside the entries var tag). To get there I needed the entry_id, but category_archive only provides the id inside the path variable:

So here’s the code I used:

{exp:weblog:category_archive  weblog="blah" style="linear" show_empty="no" }
  {categories}
     <h2>{category_name}</h2>
  {/categories}

  {entry_titles}
     {exp:perl_regex regex="%(\\d+)/$%" string="{entry_id_path=/}"  }
        <h3> <a href="http://{embed=%22parts/__id2link">{title}</a></h3>
     {/exp:perl_regex}       
  {/entry_titles}

{/exp:weblog:category_archive}

NOTES:

  1. Bugs are guaranteed!!!

  2. Thanks for Laisvunas whose plugin was purloined (but unharmed) in the making of this plugin.

  3. because the regex can have the modifier ‘e’, which allows for code execution, using this plugin potentially opens a security hole. When I have time and if it matters to people, I’ll block the use of the e modifier. until then, user’s discretion upply.

  4. actually, user’s discretion always applies 😉

       
Ingmar Greil's avatar
Ingmar Greil
29,243 posts
16 years ago
Ingmar Greil's avatar Ingmar Greil

Interesting. I always liked Perl’s way of doing regexes, so thanks for that.

ETA: is it just me, or am I missing an actual download link?

       
odspi's avatar
odspi
29 posts
16 years ago
odspi's avatar odspi

ditto!

attached above

       
odspi's avatar
odspi
29 posts
16 years ago
odspi's avatar odspi

first post and file updated.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.