Working with REGEX

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I haven't being working with REGEX for a long while so I forgot the very little information I knew about it. I'm trying to learn REGEX (still) and can't get the hang of it.



Does anyone know of any good software like REGEXbuddy (but free) that I could use to practice REGEX?

I'm also trying to get a REGEX that only accepts A-Za-z0-1.. so the REGEX for that would be...
PHP Code: [ Select ]
preg_match('/A-Za-z0-9/', $input);
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

The most efficient way to practice with regular expressions in PHP is to save a web page or ten in the same directory your PHP script is sitting, load one or more of them with file_get_contents, and try to pinpoint various sections of those webpages.

Other than that, the software you mention is the only thing I've seen of that sort in probably 5 years now.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6243
  • Loc: Seattle, WA

Post 3+ Months Ago

PHP Code: [ Select ]
preg_match('/A-Za-z0-9/', $input);


Only matches the exact string "A-Za-z0-9". You probably want a character class, which matches anything within the class, including the ranges:

PHP Code: [ Select ]
preg_match('/[A-Za-z0-9]/', $input);


(note the square brackets). However, a class represents only a single character match. To match more than one character in this class, you'd want to modify it with a * (zero or more) or a + (one or more):

PHP Code: [ Select ]
preg_match('/[A-Za-z0-9]+/', $input);


However, this is overkill, because \w is a special character that matches any word character; basically anything alphanumeric and underscores:

PHP Code: [ Select ]
preg_match('/\w+/', $input);


There's lots of online regex testers (most are written in Javascript):
http://www.bing.com/search?&q=regular+expression+tester

If I were you, I'd consider installing Perl and writing a few scripts that do various things requiring regular expressions, i.e. reading and parsing a file, modifying strings, etc. The Perl documentation has excellent regex documentation since it's so widely used.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Alright, this brings me to another question... how do I install perl?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Bogey wrote:
Alright, this brings me to another question... how do I install perl?

Ok, that was a stupid question. Which one do I install? Strawberry or ActiveState Perl?

http://www.perl.org/get.html


I built my own utility to test REGEX
PHP Code: [ Select ]
<form method="POST" action="index.php">
<p>REGEX: <input type="text" name="regex" style="width: 100%;" value="<?=isset($_POST['regex']) ? $_POST['regex'] : null;?>" /><br />
CONTENT: <textarea style="width: 100%; height: 300px;" name="content"><?=isset($_POST['content']) ? $_POST['content'] : null;?></textarea><br />
<input type="submit" name="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
<hr />
<pre><?php
if(isset($_POST['submit']))
{
    $regex = $_POST['regex'];
    $content = $_POST['content'];
   
    echo "\n\nNumber of Results found: ";
    print_r(preg_match_all($regex, $content, $match));
    echo "\n\nActual Results\n\n";
    print_r($match);
}
?></pre>
  1. <form method="POST" action="index.php">
  2. <p>REGEX: <input type="text" name="regex" style="width: 100%;" value="<?=isset($_POST['regex']) ? $_POST['regex'] : null;?>" /><br />
  3. CONTENT: <textarea style="width: 100%; height: 300px;" name="content"><?=isset($_POST['content']) ? $_POST['content'] : null;?></textarea><br />
  4. <input type="submit" name="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
  5. </form>
  6. <hr />
  7. <pre><?php
  8. if(isset($_POST['submit']))
  9. {
  10.     $regex = $_POST['regex'];
  11.     $content = $_POST['content'];
  12.    
  13.     echo "\n\nNumber of Results found: ";
  14.     print_r(preg_match_all($regex, $content, $match));
  15.     echo "\n\nActual Results\n\n";
  16.     print_r($match);
  17. }
  18. ?></pre>
I think that works...
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6243
  • Loc: Seattle, WA

Post 3+ Months Ago

I use ActiveState.
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

I use Rubular: http://www.rubular.com/
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Thanks Joebert... I installed ActivePerl... now I gotta figure out how to use it :lol:

SpooF wrote:
I use Rubular: http://www.rubular.com/

Awesome!!!! Thanks
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6243
  • Loc: Seattle, WA

Post 3+ Months Ago

Perl and PHP have very similar syntax. You probably won't have too difficult of a time picking it up.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

spork wrote:
Perl and PHP have very similar syntax. You probably won't have too difficult of a time picking it up.

No, I mean I can now start using Perl in a WAMP installation or is there something more to it after installing ActivePerl?

What I done is just ran ActivePerl.msi... it didn't ask for any input during installation (besides the license agreement, place to install it to...)

What do I do next? Just start coding in Perl?
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

You will probably have to configure Apache if you want to write websites. Otherwise you can just use the command line.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

SpooF wrote:
You will probably have to configure Apache if you want to write websites. Otherwise you can just use the command line.

How do I configure Apache and what do I configure it to?
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6243
  • Loc: Seattle, WA

Post 3+ Months Ago

Why not just write command line scripts? I don't see the point in going through the hassle of configuring Apache just to play with regular expressions. Things will be far more straightforward with plain old scripts.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I might just go ahead and learn Perl along with the regular expressions.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

So, how do I configure Apache? I just want everything to be clean... not only do I not know how to use command line scripts, but I might just learn Perl as well as REGEX.
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Well perl is probably most widely used as a command line program. Its definitely used for websites. Using it on the command line is pretty straight forward. You simply have to add a path to the executable in your PATH environment variable and your good to go. You may have told the installer to go a head and add perl to your PATh environment variable at install. You can check by opening a command line and running the below:

Code: [ Select ]
perl -v


Unless you installed perl in your home directory, if that returns some information about perl its probably already in your PATH.

If you want to use perl for websites you will have to learn a little about mod_perl and CGI.

http://perl.apache.org/
http://httpd.apache.org/docs/1.3/howto/cgi.html
http://www.thesitewizard.com/archive/ad ... ache.shtml
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6243
  • Loc: Seattle, WA

Post 3+ Months Ago

If you're going to learn Perl, you're much better off just writing simple scripts rather than web scripts.

Not everything has to revolve around a web server Bogey :)
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

The only thing I know is web development. I don't know how to use command line.

Code: [ Select ]
perl -v
That returns perl version and some information about it so I must have perl installed correctly.

How do I make simple scripts and test them without a web server? Command line? How do I run them?

Post Information

  • Total Posts in this topic: 18 posts
  • Users browsing this forum: No registered users and 121 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.