简体   繁体   中英

How to identify which delimiter comes first after word in a string using python

I have a string as:

The exam is for testing your skills. The exam includes the following:
1) Aptitude
2)synonyms
3)Reasoning

so i am using string method to identify the index of the word using the following code:

string.find('exam')

it gives me the index of the word in the string. And here i have to identify which delimiter is present at the end of each sentence. for example:

The exam is for testing your skills. [here it is '.']
The exam includes the following: [here it is ':']

so how do i identify the deimiters with which the sentence ends based on the word search?

Your problem statement is somewhat vague, as a clause can end using ",",":",";", but may not end the sentence. To revise this problem, identify the punctuation you are looking for and set as a list.

The following code identifies the starting position of all of your keywords. Then, it locates the first instance of one of your identified punctuation marks that you deem as "end of clause/sentence" and returns it.

import re

text = '''
    The exam is for testing your skills. The exam includes the following:
    1) Aptitude
    2)synonyms
    3)Reasoning'''

targets =[m.start() for m in re.finditer('exam', text)]

end_punct = ['!','.','?',':',';']

for target in targets:
    subtext = text[target:]
    print(subtext)
    for char in subtext:
        if char in end_punct:
            print(char)
            break

Sample return:

#Returns:
.
:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM