Forum Discussion

Vec84's avatar
Vec84
Contributor
8 years ago
Solved

Regular Expressions

Hello,

I have a header bar with a caption property that contains words and numbers. I want to extract the a 10 digit number from the caption string.

I've read through the documentation however I still don't understand how to implement this. The examples also don't seem to help my understanding of the syntax.

I've saved the caption property into a variable. Then created a new instance of the RegExp and tried to match method to return the numbers. I've tried loads of variations but can't seem to get it to work.

re = HIUtils.RegExpr ([0-9]);

re.Match (MYCAPTION);

Log.Message (MYCAPTION);

This is the latest I've tried however I've tried RegExp new instance and I'm just not getting the way to set it up.






  • MYCAPTION.match(/\d{10}/)[0]

    extracts a 10-digit number from the variable MYCAPTION.

13 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    MYCAPTION.match(/\d{10}/)[0]

    extracts a 10-digit number from the variable MYCAPTION.

    • Vec84's avatar
      Vec84
      Contributor

      Many Thanks Helen,

       

      You are the Testcomplete Chief !

    • Marsha_R's avatar
      Marsha_R
      Champion Level 3

      Is the number always in the same position in the string or is the "Motor - Ad" part a random size?

    • Marsha_R's avatar
      Marsha_R
      Champion Level 3

      Will there be any other numbers before the 10 digit one?

    • Marsha_R's avatar
      Marsha_R
      Champion Level 3

      What I did for something similar was to use aqString.Trim to get all the spaces out of the string and then loop through it a character at a time looking for a digit.  That will get you the position of the first digit.  Then you can use aqString.Substring to get the 10 digits.

       

      I never found a way to use the regular expressions for this.  Looks like you can use them to compare but not for string manipulation.

  • Vec84's avatar
    Vec84
    Contributor
    I'll give that a try in the morning...Thanks very much for your help Marsha.
  • NisHera's avatar
    NisHera
    Valued Contributor

    assuming you are using python below will work

     

    Log.message(re.sub("\D", "", "Motor - Ad 1112223445 name name"))
  • Vec84's avatar
    Vec84
    Contributor
    Hello NisHera,

    I'm using jscript but I will attempt to translate from python.

    Thanks for your reply