How to find name, id, type in string using re?
string = "Wacom Bamboo Connect Pen stylus   id: 15  type: STYLUS"
Expected Result:("Wacom Bamboo Connect Pen stylus", 15, "STYLUS")
How to find name, id, type in string using re?
string = "Wacom Bamboo Connect Pen stylus   id: 15  type: STYLUS"
Expected Result:("Wacom Bamboo Connect Pen stylus", 15, "STYLUS")
 
    
    Use re.findall. The regex:
r'^(.*\S)\s+id:\s*(\d+)\s+type:\s*(.+)' means:
^ : start of the string.
.* :any character, repeated 0 or more times.
\S : non-whitespace character.
\s+ : whitespace character, repeated 1 or more times.
\d+ : any digit, repeated 1 or more times.
(PATTERN) : capture the patterns and return it. Here we capture 3 patterns.
import re
string = "Wacom Bamboo Connect Pen stylus   id: 15  type: STYLUS"
lst = re.findall(r'^(.*\S)\s+id:\s*(\d+)\s+type:\s*(.+)', string)
# The first match (list element) is a tuple. Extract it:
lst = list(lst[0])
lst[1] = int(lst[1])
print(lst)
# ['Wacom Bamboo Connect Pen stylus', 15, 'STYLUS']
 
    
    To match the first string before id you need:
.*(?=(id:))
To match the id you need:
(?<=id:.*)(\d*)(?=.*type)
To match the type you need:
(?<=type:.*)(\w+)
I would suggest you have a look at lookaheads and lookbehinds.
