python - The same regex pattern that works fine doesn't work implemented into a Class -


क्यों यह कोड एक परिणाम प्राप्त करता है:

  आयात पुनः input_file = open ("nota_simple Txt ", mode = 'r', एन्कोडिंग = 'यूटीएफ -8') text_to_search = input_file.read () पैटर्न = re.compile (" (? & Lt; = FINCA Nº:)। * ") परिणाम = pattern.search ( Text_to_search) प्रिंट (result.group ())  

लेकिन यह दूसरा कुछ भी मेल नहीं खाता है? (कोई भी परिणाम नहीं है)

  फिर से सम्पत्ति का नंबर (ऑब्जेक्ट) आयात करें: regex_pattern = re.compile ("(? & Lt; = FINCA Nº:)। *") Def __init __ (स्वयं, str ): self.text_to_search = str self.text_found = "" डीईएफ़ search_p_number (स्वयं): matched_p_number = PropertyNumber.regex_pattern.search (self.text_to_search) प्रिंट (self.text_to_search) प्रिंट (matched_p_number) self.text_found = matched_p_number.group () डीईएफ़ मुख्य (): file_input = खुला ( "nota_simple.txt", मोड = 'आर', एन्कोडिंग = 'utf-8') property_report = file_input.read () PROPERTY_NUMBER = PropertyNumber (property_report) प्रिंट (property_number.search_p_number ()) अगर __name__ == '__main__': मुख्य ()  

मैंने ऐसा किया है, सिर्फ एक पाठ को लागू करने के लिए आवश्यक डेटा को संभालने के लिए क्लास को लागू करना है।

इसलिए रिजक्स पैटर्न मुद्दा नहीं है, मेरा मानना ​​है कि ओओपी कार्यान्वयन में है (मैं ओओपी में नॉओ हूँ)।

  #! / Usr / bin / env python # - * - कोडन: यूटीएफ -8 - * - # सुझाए गए बदलाव आयात पुनः क्लास प्रॉपर्टी नम्बर (ऑब्जेक्ट): def __init __ (आत्म, स्ट्र, पैटर्न = "(? & Lt; = FINCA Nº:)।" "): Self.regex_pattern = re.compile (pattern) self.text_to_search = str self.text_found = "" डीईएफ़ search_p_number (स्वयं): matched_p_number = self.regex_pattern.search (self.text_to_search) प्रिंट (self.text_to_search) प्रिंट (matched_p_number) self.text_found = matched_p_number.group () वापसी self.text_found डीईएफ़ मुख्य (): file_input = खुला ( "nota_simple.txt", मोड = 'आर', एन्कोडिंग = 'utf-8') property_report = file_input.read () PROPERTY_NUMBER = PropertyNumber (property_report) प्रिंट (property_number.search_p_number ()) अगर __name__ == '__main__ ': मुख्य ()  

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -