Paul
Paul

Reputation: 5984

Method to find word in string

I am searching a string, there will be a line beginning with ssid and I need to find the word directly after that. So an example is "ssid home", home would be the word I wish to return. I have done this in a roundabout way, seems very messy, what way should I do this, a regex perhaps or is there a way to tidy up what I have done?

a = """
!
interface blah
a
ssid test
v
v
"""

b = a.split("\n")
matches = [x for x in b if "ssid" in x]
matches = [i.split() for i in matches]
print matches[0][1]

Upvotes: 2

Views: 157

Answers (4)

thefourtheye
thefourtheye

Reputation: 239653

flag, result = False, []
for item in a.split():
    if flag:
        result.append(item)
        flag = False
    if item == "ssid":
        flag = True
        continue
return result

Lets do some time comparisons :)

a = """
!
interface blah
a
ssid test1
v
ssid test2
v
ssid test3
"""
import re
p = r'(?<=ssid )\S+'
def ray(a):
    return re.findall(p, a)

def abhijit(a):
    return [e.split(None, 1)[0] for e in a.split("ssid")[1:]]

def thefourtheye(a):
    flag, result = False, []
    for item in a.split():
        if flag:
            result.append(item)
            flag = False
        if item == "ssid":
            flag = False
            continue
    return result

from timeit import timeit
print "Ray", timeit("ray(a)", "from __main__ import ray, a, p")
print "Abhijit", timeit("abhijit(a)", "from __main__ import abhijit, a")
print "thefourtheye", timeit("thefourtheye(a)", "from __main__ import thefourtheye, a")

Output

Ray 2.4214360714
Abhijit 1.39024496078
thefourtheye 1.11726903915

Upvotes: 1

neil
neil

Reputation: 3645

a = """
!
interface blah
a
ssid test
v
v
"""

for line in a.split("\n"):
   if line.startswith("ssid"):
      result = line.split()[1]
      break

Using the for loop allows breaking out when the matching line is found rather than checking all the remaining ones. Whether that is worthwhile depends on the length of the expected data.

Upvotes: 1

Ray
Ray

Reputation: 2508

a = """
!
interface blah
a
ssid test1
v
ssid test2
v
ssid test3
"""
p = r'(?<=ssid )\S+' # non-whitespace character chunk after ssid
match = re.findall(p, a)

This will give you: ['test1', 'test2', 'test3']

Upvotes: 5

Abhijit
Abhijit

Reputation: 63777

Split your string based on your key, ssid and then after discarding the first partition, iterate over the remaining partitions accepting only the first word and discarding the rest.

>>> a = """
!
interface blah
a
ssid test1
v
ssid test2
v
ssid test3
"""
>>> [e.split(None, 1)[0] for e in a.split("ssid")[1:]]
['test1', 'test2', 'test3']

A similar regex solution would be

>>> re.findall("ssid\s+(\w+)", a)
['test1', 'test2', 'test3']

Upvotes: 3

Related Questions