Forum Discussion

Michael_B's avatar
Michael_B
Occasional Contributor
2 years ago

Python code to return a substring using regular expressions?

Hello - I'm attempting to return the 5 digits after "ID:" using regular expressions. I have an expression that works (see attachment), but cannot seem to get testcomplete to return it. When I get this script to work with a regular expression, it will help my team with these types of issues without reinventing the wheel with custom wildcard setups and such. Can you see what I am doing wrong? Thanks for your help!

def RegExRSearch():
import re
str = "Smith, Tom\nID:\n69777\nLocation:\nDenver"
# Define regular expression pattern.
regexp = "ID:\n(.{5})"
# Perform replace operation
result = re.search(regexp, str)
Log.Message(result)

  • Change the last line to,

     

    Log.Message(result[0])

    and you can work from there on.

     

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Change the last line to,

     

    Log.Message(result[0])

    and you can work from there on.

     

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    In Python, it will output the results like so

    import re
    re.findall(r'\d+', "hello 42 I'm a 32 string 30")
    > ['42', '32', '30']

    but TestComplete won't print the array ['42', '32', '30'] in Log.Message(), which is annoying!