[Python] Save sentences that would have included a specific word

Asked 1 years ago, Updated 1 years ago, 39 views

Hello.

If a specific word is included, I'm trying to write a code to save the sentence to firebase!

I don't have a clue

For example, if there's a sentence that says, "Hashcode is good!" I'm just trying to narrow it down.

How can I make a way to throw away the word "hash code" even if there are multiple sentences, and save it if there are.?

Please give me your advice.

python firebase

2022-09-22 19:50

1 Answers

"I like it!" "I like you!" "I like it!" "I like it!" "I like the hash code!" "I like it!" Do you want to take a hash code or a sentence like "I like it

r"(Good![\w\s]*Hashcode|Hashcode[\w\s]*Good!) How about using this regular expression? The sample is as follows:

# coding=utf8
# # the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r (like![\w\s]*hashcode|hashcode[\w\s]*like!)

test_str = ("Like!", \n"
    "I like you!" \n"
    "Throw away the sentence ""Nice, yum!"""
    "Hashcode is good!", \n"
    "Good! Hashcode", \n"
    "I like hash codes!" \n"
    "Do you want to take this kind of sentence?")

subst = ""

# # You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.UNICODE)

if result:
    print (result)


2022-09-22 19:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.