New
New

Reputation: 25

Python - Extract text from string

What are the most efficient ways to extract text from a string? Are there some available functions or regex expressions, or some other way?

For example, my string is below and I want to extract the IDs as well as the ScreenNames, separately.

[User(ID=1234567890, ScreenName=RandomNameHere), User(ID=233323490, ScreenName=AnotherRandomName), User(ID=4459284, ScreenName=YetAnotherName)]

Thank you!

Edit: These are the text strings that I want to pull. I want them to be in a list.

Target_IDs = 1234567890, 233323490, 4459284 Target_ScreenNames = RandomNameHere, AnotherRandomName, YetAnotherName

Upvotes: 2

Views: 10281

Answers (3)

Darkstarone
Darkstarone

Reputation: 4730

The regex I'd use would be:

(?:ID=|ScreenName=)+(\d+|[\w\d]+)

However, this assumes that ID is only digits (\d) and usernames are only letters or numbers ([\w\d]).

This regex (when combined with re.findall) would return a list of matches that could be iterated through and sorted in some fashion like so:

import re

s = "[User(ID=1234567890, ScreenName=RandomNameHere), User(ID=233323490, ScreenName=AnotherRandomName), User(ID=4459284, ScreenName=YetAnotherName)]"
pattern = re.compile(r'(?:ID=|ScreenName=)+(\d+|[\w\d]+)');

ids = []
names = [] 

for p in re.findall(pattern, s):
    if p.isnumeric():
        ids.append(p)
    else:
        names.append(p)

print(ids, names)

Upvotes: 0

Synedraacus
Synedraacus

Reputation: 1045

It depends. Assuming that all your text comes in the form of

TagName = TagValue1, TagValue2, ...

You need just two calls to split.

tag, value_string = string.split('=')
values = value_string.split(',')

Remove the excess space (probably a couple of rstrip()/lstrip() calls will suffice) and you are done. Or you can take regex. They are slightly more powerful, but in this case I think it's a matter of personal taste.

If you want more complex syntax with nonterminals, terminals and all that, you'll need lex/yacc, which will require some background in parsers. A rather interesting thing to play with, but not something you'll want to use for storing program options and such.

Upvotes: 0

Transhuman
Transhuman

Reputation: 3547

import re
str = '[User(ID=1234567890, ScreenName=RandomNameHere), User(ID=233323490, ScreenName=AnotherRandomName), User(ID=4459284, ScreenName=YetAnotherName)]'
print 'Target IDs = ' + ','.join( re.findall(r'ID=(\d+)', str) )
print 'Target ScreenNames = ' + ','.join( re.findall(r' ScreenName=(\w+)', str) )

Output : Target IDs = 1234567890,233323490,4459284 Target ScreenNames = RandomNameHere,AnotherRandomName,YetAnotherName

Upvotes: 2

Related Questions