1

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.

3
  • What do you think is '([a-z])\w+' % re.escape(net_card)? (Hint: It is not what you think, and it is the expression that causes the problem; everything else is irrelevant.) Commented Sep 1, 2017 at 3:26
  • What are you trying to match ? what did you expect the result to be for NAME=ens32 ? Commented Sep 1, 2017 at 3:28
  • I'm trying to get ens32 part Commented Sep 1, 2017 at 4:23

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.