0
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.

Elisha
  • 4,811
  • 4
  • 30
  • 46

2 Answers2

3

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

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • Strictly speaking, it doesn't seem correct to use `find_all()` for finding just one first match. Just think about it: for example, would you fetch all of your data from a table in order to get just one first row?..though it makes things work here, yes. – alecxe Jan 16 '14 at 06:47
2

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:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195