-1

I have a Python code that read text files and allow the user to enter a searched word where the system do the matching task.

what i want is to be able to find the entered word if exist in the text no matter how its written (upper, lower, capitalized), without using Regular expression.

i know that the code i wrote is not correct because the output is the hall text splited in a list.

where the result must be:

self.varStr = user-input repeated as many as it exist in the text in any mentioned case.

example :

  • user-input = charl
  • existing word: Charles,Charles

so what i did is :

textString contains the hall text.

Les hiboux Charles Baudelaire

Cycle 3 *

POESIE

Sous les ifs noirs qui les abritent Les hiboux se tiennent rangés Ainsi que des dieux étrangers Dardant leur œil rouge. Ils méditent.

Sans remuer ils se tiendront Jusqu'à l'heure mélancolique Où, poussant le soleil oblique, Les ténèbres s'établiront.

Leur attitude au sage enseigne Qu'il faut en ce monde qu'il craigne Le tumulte et le mouvement ;

L'homme ivre d'une ombre qui passe Porte toujours le châtiment D'avoir voulu changer de place.

Les Fleurs du Mal 1857

Charles Pierre Baudelaire (1821 – 1867) est un poète français.

code:

   x=self.lineEditSearch.text()
            print(x)
            textString=self.ReadingFileContent(Item)

            #self.varStr =[c for c in textString if c.islower() or c.isupper() or c.capitalize()]            
            self.varStr =[x for x in textString.split(" ") if x.islower() or x.isupper() or x.capitalize()]            

            print(self.varStr)
Propy Propy
  • 57
  • 10

1 Answers1

1

Your version isn't working because you are iterating through all the words in textString and then adding them to the new list if they are either lower case, upper case or capitalized. It adds all the words it finds because your if statement covers just about every state a word is likely to be in.

What I would do is compare the lower case version of each word in textString to the lower case version of the word you are looking for (x), like this:

self.varStr = [i for i in textString.split(" ") if i.lower() == x.lower()] 

The method lower() doesn't change the case of the string it finds so you get a list with the strings you want in the original case.

Based on the further information given, you want to match part of the string entered by the user, to the actual string that appears in the text, in your example "charl" matching "Charles" and "Charles", in which case you can do something like this:

self.varStr = [i for i in textString.split(" ") if i.lower()[:len(x)] == x.lower()]

It will work in the example you gave but only if the entered text matches the start of the word. If you are trying to match parts of a string to strings you are better off figuring out how regular expressions in python work.

srk
  • 566
  • 1
  • 4
  • 4