user3200938
user3200938

Reputation: 1

Python: RE issue with "re.findall()"

string = "RegisterParameter uri wub {"
RegisterName = re.findall("RegisterParameter uri ([^ ]*) {",string)

print 'RegisterName is :',RegisterName

See the above code. Here i want to find register name in the string i.e wub by regular expression. I have written the RE for that. If you run this code it will give the output like ['wub'] ,but i want only wub not bracket or quote. So what modifications to be done over here.

Many thanks for your help.

Upvotes: 0

Views: 95

Answers (2)

alecxe
alecxe

Reputation: 473873

You can use re.search() (or re.match() - depends on your needs) and get the capturing group:

>>> import re
>>> s = "RegisterParameter uri wub {"
>>> match = re.search("RegisterParameter uri ([^ ]*) {", s)
>>> match.group(1) if match else "Nothing found"
'wub'

Also, instead of [^ ]*, you may want to use \w*. \w matches any word character.

See also:

Upvotes: 2

Christian Tapia
Christian Tapia

Reputation: 34146

RegisterName is a list with just one str element. If the issue is just printing you could try:

print 'RegisterName is :', RegisterName[0]

Output:

RegisterName is : wub

PS:

  • When you are not sure of the type of a variable try printing it:

    print type(RegisterName)
    
  • I would recommend you to use Python conventions, identifiers with names like SomeName are often used as names of classes. For variables, you could use some_name or register_name

Upvotes: 3

Related Questions