Python code to return a substring using regular expressions?
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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)
Solved! Go to Solution.
- Labels:
-
Script Tests
-
Web Testing
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Change the last line to,
Log.Message(result[0])
and you can work from there on.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
