Perl Regex tester.

Discussion in 'CIW Certifications' started by stuPeas, Oct 17, 2007.

  1. stuPeas

    stuPeas Megabyte Poster

    774
    12
    76
    Ive written a little Perl script to help me with my Perl Regular Expressions revision. You type in the search pattern you wish to match at the prompt (without the delimitters), and then type in the string you want to test for a match.
    This is a command line script. It could be written differently by using a function, but I was also working on named loops so I decided to combine the two. You obviously need Perl installed for it to work.

    You may delete the shebang line for windows users (It doesnt actualy DO anything), or change it for NIX users.

    Use it at you lesiure my fellow CIW'ers. :biggrin




    Code:
    #!c:\perl\bin\perl.exe
    
    
    LOOP:{
        print("\nInput a Regular Expression Search Pattern\n(not including delimiters): ");
        $reg = <STDIN>;
        chomp ($reg);
    
    
        if ($reg eq "exit"){
          print("\nGoodbye!!\n");
          last LOOP;
        }
        print("\nInput a String to search for a Match: ");
        $myString = <STDIN>;
        chomp ($myString);
    
    
    
    
        if ($myString =~ m/$reg/){
    
               print("\nYour pattern matched the string \"$myString\"\n");
               print("The characters matched were \"$&\"\n");
         }
         else{
              print("\nSorry, No Match found in the string \"$myString\"\n");
         }
    
         redo LOOP;
        
    }
           
    EDIT: Forgot to say; The program will keep waiting for user input after each test, so type "exit" at the prompt for input, to shut it down.

    Edit 2: save this file as whatever you want, then change to its directory and call it by typing "perl scriptname.pl"

    EDIT 3: Now works with back references
     
    Certifications: C&G Electronic, CIW Associate (v5).
    WIP: CIW (Website Design Manager)

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.