Home > Software engineering >  Python regex for select everything from my selected work to end of the line
Python regex for select everything from my selected work to end of the line

Time:08-19

I have this string already separate by lines demarcation (used strip("\n")):

hello hola ciao morning night tardes description "this is a example CES132165"

hello hola ciao afternoon night mediodia description "this is a example AScasdf"

My goal is to take only the description itself until the jump line. Result should be:

description "this is a example CES132165"

description "this is a example AScasdf"

CodePudding user response:

You may use re.search() here for a concise solution:

inp = 'hello hola ciao morning night tardes description "this is a example CES132165"'
output = re.search(r'"(.*?)"', inp).group(1)
print(output)  # this is a example CES132165

CodePudding user response:

You don't need to use regex for this. You can use the string method find to find where in the line your description begins, and then read the rest of the line.

output = []
for line in mystring.split('\n'):
    description_index = line.find('description "')
    output.append(line[description_index:])
print(output)
#output
['description "this is a example CES132165"',
 'description "this is a example AScasdf"']

CodePudding user response:

You can use regex for this. The following regex pattern should work r'"([A-Za-z0-9_\./\\-]*)"'

import re
 text = 'hello hola ciao morning night tardes description "this is a example CES132165"'
 pattern = r'"([A-Za-z0-9_\./\\-]*)"'
 m = re.match(pattern, text)
 print m.group()

CodePudding user response:

Simple way using slicing, 'description' is 11 letters long:

s = 'hello hola ciao morning night tardes description "this is a example CES132165"'

s = s[s.find('description')   11:]

print(s)

Output:

"this is a example CES132165"
  • Related