[Solved] How to find data in string? [closed]


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']

2

solved How to find data in string? [closed]