Michael_B
2 years agoOccasional Contributor
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.