Reputation: 25
guys i hope you can give me a hand with this:
Im trying to find a match on a variable value: net_card is a string
net_card = salida.read()
regex = re.compile('([a-z])\w+' % re.escape(net_card))
if i run this code it show me this error:
regex = re.compile('([a-z])\w+' % re.escape(net_card))
TypeError: not all arguments converted during string formatting
I haven't found a way to solve this, even with scape characters.
now if i do this:
net_card = salida.read()
match = re.search('([a-z])\w+', net_card)
whatIWant = match.group(1) if match else None
print whatIWant
it shows me just (e) in the output even when the value of net_card is NAME=ens32.
Upvotes: 1
Views: 72
Reputation: 4130
Your regex, ([a-z])\w+
, will match a single character in the range a-z
as the first group, and match the rest of the string as [a-zA-Z0-9_]+
. Instead, match the two groups of \w+
(which is [a-zA-Z0-9_]+
in evaluation), separated by an equal sign. Here's an expression:
(\w+)=(\w+)
In practice (if you don't care about "NAME"
), you can remove the first group and use:
net_card = salida.read()
match = re.match('\w+=(\w+)', net_card)
print(match.group(1) if match else None)
Which will output ens32
.
Upvotes: 2