locked
Regular Expression in an Extraction Rule RRS feed

  • Question


  •  Funny - I did Perl for years, but I can't get the Regular Expression extraction rules to work : )

    Here's a snippet of javascript being returned:

    AddNewPageCallback('1234567')

    What I want is the page id, that number between the 's.

    I've got a little extraction rule -

    ExtractRegularExpression extractionRule3_1 = new ExtractRegularExpression();

    extractionRule3_1.Required = true;

    extractionRule3_1.RegularExpression = "AddNewPageCallback('.+')";

            extractionRule3_1.ContextParameterName = "page_id";

    ...I would expect that rule to save off that string of digits and put it in this.Context with a key of "page_id".

    But it doesn't - obviously, to those of you who know what's wrong with it.

    I would like to join you folks who know what's wrong with it. Desperately : ) - the last time I got to this point, I gave up and wrote a custom extraction rule to scrape the string; I'd rather learn what i'm doing wrong.

    thanks,
    jim p.

    Regular



    • Moved by Bill.Wang Wednesday, February 11, 2009 8:45 AM Extraction rule should be a web test question. (Moved from Visual Studio Team System - Testing to Visual Studio Team System - Web and Load Testing)
    Monday, February 9, 2009 9:11 PM

Answers

  • With the Extract Regular Expression rule, you need to use lookahead and lookbehind in the RegEx.

    The expression does what you want: (?<=AddNewPageCallback\(')\d*(?='\))

     

     

    • Proposed as answer by Lewis Horowitz Wednesday, February 11, 2009 2:18 PM
    • Marked as answer by Bill.Wang Thursday, February 12, 2009 2:57 AM
    Wednesday, February 11, 2009 2:18 PM

All replies

  • Hi Jim
     

    It seems you can't do this.

     

    I checked the code for ExtractRegularExpression with Reflector. It extracts the matched value of the specified expression. So the whole matched string will be extracted. I think it will be better if we can configure which group to return.


    Please mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, February 11, 2009 8:44 AM
  • With the Extract Regular Expression rule, you need to use lookahead and lookbehind in the RegEx.

    The expression does what you want: (?<=AddNewPageCallback\(')\d*(?='\))

     

     

    • Proposed as answer by Lewis Horowitz Wednesday, February 11, 2009 2:18 PM
    • Marked as answer by Bill.Wang Thursday, February 12, 2009 2:57 AM
    Wednesday, February 11, 2009 2:18 PM
  • Thanks for Lewis's help. You are right.
    Please mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, February 12, 2009 2:56 AM
  • Hey Lewis & Bill,

    I applied your suggestion:

    extractionRule3_1.RegularExpression = "(?<=AddNewPageCallback\(')\d*(?='\))";

    but VSTS Test Edition 2008 displays 'Unrecognized escape sequence' after the backslashes...I added a second backslash to each and the compiler seemed to accept it but not sure if that's the right thing to do?

    Thanks,
    -Bruce
    Thursday, February 12, 2009 4:10 PM
  • I tried the expression in a RegEx tester so I needed to put in the backslashes before the parens for it to work. VSTS may not need the backslash before the parens. I would suggest you try it without the backslashes and see if you get the number. If not, try with the 2.
    Thursday, February 12, 2009 5:34 PM