Forum Discussion

royd's avatar
royd
Regular Contributor
7 years ago

Regular Expression to extract a 'word' from string. Need help.

I am trying to extract success code from email message. I was able to do so with “aqString.SubString” method, thanks to Shankar R. I have been practicing Regular Expression and wanted to attempt extracting the access code using Regular Expression. The security code email message contains the following message:

 

Your AMG - Facility forms access code is 22RP3DPM. You will receive a link to your forms using your primary email address. You will need this access code to use the forms link. Further instructions will be available after using the access code to access the forms.

 

 

With Regular Expression, I was able to isolate the access code, this is what I came up with:

   \b[0-9A-Z]{8}\b

 

I can access the entire message with following:

 

let msgText = page.FindChild(“idStr”, “msg_panel”, 10).contentText;

How can I isolate the access code using Regular Expression? I really appreciate any help I can get.

 

Thanks

 

Dave

 

 

  • Okay, I have figured it out! Took a wild guess and it worked. The code below gave me what I was after.

     

     

    let msgBody = "Your AMG - Facility forms access code is 22RP3DPM. You will receive a link to your forms using your primary email address. You will need this access code to use the forms link. Further instructions will be available after using the access code to access the forms.";
    let secCode = msgBody.match(/[0-9A-Z]{8}/); // the '/' before and after [0-9A-Z]{8} specifies it as Regular Expression
    
    Log.Message("Security code is: " + secCode);

     

    Log reads as: "Security code is: 22RP3DPM"

     

    Hope this comes handy for someone else in my situation. :)

  • royd's avatar
    royd
    Regular Contributor

    Okay, I have figured it out! Took a wild guess and it worked. The code below gave me what I was after.

     

     

    let msgBody = "Your AMG - Facility forms access code is 22RP3DPM. You will receive a link to your forms using your primary email address. You will need this access code to use the forms link. Further instructions will be available after using the access code to access the forms.";
    let secCode = msgBody.match(/[0-9A-Z]{8}/); // the '/' before and after [0-9A-Z]{8} specifies it as Regular Expression
    
    Log.Message("Security code is: " + secCode);

     

    Log reads as: "Security code is: 22RP3DPM"

     

    Hope this comes handy for someone else in my situation. :)

    • karkadil's avatar
      karkadil
      Valued Contributor

      Using regular expressions for this particular case looks good because the word you want to extract contains numbers and its length is constant.

      However, if you'd need to extract some other text (e.g. a person name from email body), regular expressions will not help you. In this case you can use a set of JScript methods split/join/slice. Here are 2 ways of how you can also extract the access code in your example:

       

      // extracts 8 characters after "code is " substring
      var code = s.split('access code is ')[1].slice(0, 8); 
      
      // extracts a substring of any length between "code is " substring and next "." character
      var code = s.split('access code is ')[1].split('.')[0]; 
      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Might even be easier than that.  I'm assuming that the text in question is a standardized format with the only variant being the security code.  If you "split" the string on the space character, it's the 8th word every time.

         

        var code = s.split(' ')[7];
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    My thinking is this:

     

    If it's not broke, don't fix it.  If you have it working with aqString.SubString, do you need to do anything with regular expressions? 

  • royd's avatar
    royd
    Regular Contributor

    Shankar

     

    If you are talking of  regular expression, I have tested \b[0-9A-Z]{8}\b to select the access code and it works. It  is a combination of 8 capital letters and numbers combination (random). I was wondering if there is a way to assign that code to a variable.

     

    Thanks.

     

    robert 

     

    The "aqString.SubString" works flawlessly, I am just trying to see if the regex can be used in a similar situation.  :)

  • shankar_r's avatar
    shankar_r
    Community Hero

    Is it going to any format like below? if so we can try something

     

    <number[2]><number[2]><character[R]><character[P]><number[3]><character[D]><character[P]><character[M]>

     

     

  • royd's avatar
    royd
    Regular Contributor

    Very stimulating conversation! :D I love it! I am always learning new thing here. 

     

    gennadiy 

    Thank you for showing me the split/join/slice method.

     

    Thank everyone for very informative discussion! :)