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
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)
在空白处分割字符串,然后打印第二个子字符串,即
print(string.split(' ')[1])
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.