1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Regex tester Problem

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

  1. stuPeas

    stuPeas Megabyte Poster

    774
    12
    76
    O.K guys, Lets see if we have any Perl coders among us.

    So my little regex tester program works fine for SIMPLE search patterns but as soon as a user types in a search pattern that includes "back references" it all falls apart. For those of you who can see what the code does, you will see that this is because of the elements I have highlighted in red ( in the code block below).

    The problem is that the brackets (in red) I have used to add functionality (so the user can see exactly what matched) mean that if a user types in the Regex m/\w+(.)\1\w+/, then the \1 in their pattern will eroneously apply to the WHOLE pattern (because of the line ,if($myString =~ m/($reg)/) in the script) INSTEAD OF simply applying to the bracketed symbol in the users pattern (m/\w+(.)\1\w+/)

    A way around this is for the user to begin any back references they require from the number 2 (\2). this has the effect of refering only to the references that the user included in their own, typed-in, search pattern.

    What I want from one of you brilliant people is a way that I can INCREMENT (in my script) any digit that follows the \ (backslash) symbol in the users search pattern BEFORE applying it to to final search. The only problem is that this involves doing a search in the users entered pattern for this m/\d/ BUT THIS ACTUALY MEANS "ANY DIGIT". so if i try to escape the backslash so I am trying to match the explict symbol \, I get m/\\d/, which just doesn work.



    Code:
    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/[COLOR="Red"]([/COLOR]$reg[COLOR="Red"])[/COLOR]/){
    
               print("\nYour pattern matched the string \"$myString\"\n");
               print("The characters matched were \"[COLOR="Red"]$1[/COLOR]\"\n");
         }
         else{
              print("\nSorry, No Match found in the string \"$myString\"\n");
         }
    
         redo LOOP;
        
    }
    Any Ideas people???
     
    Certifications: C&G Electronic, CIW Associate (v5).
    WIP: CIW (Website Design Manager)
  2. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    I haven't looked very deeply at this, so apologies if I've misunderstood something.

    Your basic problem is that you are trying to use back references in your code, which collides with any in the regex string.

    Why not use $MATCH instead?

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  3. stuPeas

    stuPeas Megabyte Poster

    774
    12
    76
    Any chance of a quick example of its use Harry?? I've not seen it before. :D
     
    Certifications: C&G Electronic, CIW Associate (v5).
    WIP: CIW (Website Design Manager)
  4. stuPeas

    stuPeas Megabyte Poster

    774
    12
    76
    Simplified Question.

    In essence what I want to do is:
    Find EVERY instance (in a users input string) of the symbol "\" followed by a digit. I then want to increment each digit by 1. Thats it!! sounds simple!!
     
    Certifications: C&G Electronic, CIW Associate (v5).
    WIP: CIW (Website Design Manager)
  5. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    Remove the parentheses in the testing line, and replace $1 with $& (which is the short form of $MATCH).

    And get a decent book on Perl! :biggrin This can be found in the Camel book.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  6. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    Don't need to - see above! :biggrin

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  7. stuPeas

    stuPeas Megabyte Poster

    774
    12
    76
    You the man Harry!!, works like a charm now. I cant believe that I've stopped revision to work on a program that was meant to HELP with revision. How dumb is that!!!.

    Cheers mate. :D
     
    Certifications: C&G Electronic, CIW Associate (v5).
    WIP: CIW (Website Design Manager)

Share This Page

Loading...