Reputation: 1
I'm writing a python script that will extract specific parts of lines in a text file and print them out. The part of that line is always between two specific characters. For example, I need to print everything between "Ghost: "
and "("
.
I've tried importing re
and using it to search a string for characters, but I failed.
The file looks something like this:
Ghost: john.doe.1 {} ...
Ghost: john.walker.4 {} ...
Ghost: john.johnny.3 {} ...
Ghost: john.craig.6 {} ...
...
I'm expecting something like this:
john.doe.1
john.walker.4
john.johnny.3
john.craig.6
Upvotes: 0
Views: 611
Reputation: 6639
Using regex, you can do:
re.search('((?:[a-z][a-z]+))(\\.)((?:[a-z][a-z]+))(\\.)(\\d+)', text).group()
Output:
john.doe.1
john.walker.4
john.johnny.3
john.craig.6
Logic:
'((?:[a-z][a-z]+))' - looks for word (a-z)
'(\\.)' - looks for dot
'((?:[a-z][a-z]+))' - looks for word (a-z)
'(\\.)' - looks for dot
'(\\d+)' - looks for digit (0-9)
Upvotes: 1
Reputation: 820
you can use split method and extract the required info
word="Ghost: john.doe.1 {} ..."
word=word.split(":")[1]
print(word.split()[0])
OUTPUT:
john.doe.1
Upvotes: 0
Reputation: 3318
split the string on whitespace and then print 2nd substring i.e
print(string.split(' ')[1])
Upvotes: 0