कोर्स
यदि आप हमारा निःशुल्क Intro to R कोर्स करना चाहते हैं, तो यहाँ लिंक है।
स्ट्रिंग्स से संख्यात्मक मान निकालना
स्ट्रिंग्स से नंबर निकालना एक सामान्य कार्य है, खासकर जब आप असंरचित डेटा या लॉग फ़ाइलों के साथ काम कर रहे हों।
मान लीजिए आपके पास निम्न स्ट्रिंग है: 'the recipe calls for 6 strawberries and 2 bananas'.
इस स्ट्रिंग से 6 और 2 निकालना उपयोगी होगा ताकि बाद में स्ट्रॉबेरी से केले के अनुपात की तुलना करते समय उन्हें सुरक्षित रखा जा सके।
एक से अधिक नंबर (या सटीक रूप से कहें तो, एक से अधिक पैटर्न मैच) निकालने के लिए रेगुलर एक्सप्रेशन का उपयोग करते समय, आप re.findall() फ़ंक्शन का उपयोग कर सकते हैं। डैन ने वीडियो में इस पर चर्चा नहीं की, लेकिन इसका उपयोग सीधा है: आप re.findall() को एक पैटर्न और एक स्ट्रिंग पास करते हैं, और यह मैचों की एक सूची लौटाता है।
निर्देश
reइम्पोर्ट करें।- ऐसा पैटर्न लिखें जो निम्न स्ट्रिंग में सभी नंबर ढूँढ ले:
'the recipe calls for 10 strawberries and 1 banana'. ऐसा करने के लिए:re.findall()फ़ंक्शन का उपयोग करें और इसमें दो आर्ग्युमेंट पास करें: पहले पैटर्न, फिर स्ट्रिंग।\dडिजिट्स ढूँढने के लिए आवश्यक पैटर्न है। इसके बाद+लगाएँ ताकि पिछला एलिमेंट एक या अधिक बार मैच हो। यह सुनिश्चित करता है कि10को एक संख्या माना जाए, न कि1और0के रूप में।
- मैचों को प्रिंट करें ताकि पुष्टि हो सके कि आपके रेगुलर एक्सप्रेशन ने
10और1वैल्यूज़ ढूँढ ली हैं।
# Import the regular expression module
____
# Find the numeric values: matches
matches = re.findall('____', '____')
# Print the matches
print(____)
# Import the regular expression module
import re
# Find the numeric values: matches
matches = re.findall('\d+', 'the recipe calls for 10 strawberries and 1 banana')
# Print the matches
print(matches)
Ex().test_import('re')
Ex().test_correct(test_object('matches'), test_function('re.findall'))
Ex().test_function('print')
success_msg('Excellent work - your regular expression successfully extracted the numeric values 10 and 1 from the string!')
- मॉड्यूल
xको इम्पोर्ट करने के लिए कमांडimport xका उपयोग करें। re.findall()का पहला आर्ग्युमेंट\d+होना चाहिए, और दूसरा आर्ग्युमेंट यह स्ट्रिंग होनी चाहिए:'the recipe calls for 10 strawberries and 1 banana'.matchesप्रिंट करने के लिए दिए गएprint()फ़ंक्शन का उपयोग करें।
यदि यह समझ में आ गया है तो अगले अभ्यास पर बढ़ें! यदि नहीं, तो यहाँ एक ओवरव्यू वीडियो है।
Python में रेगुलर एक्सप्रेशन का उपयोग करके स्ट्रिंग्स साफ करने पर ओवरव्यू वीडियो
पैटर्न मैचिंग
इस अभ्यास में, आप अपने रेगुलर एक्सप्रेशन कौशल का अभ्यास जारी रखेंगे। प्रत्येक दी गई स्ट्रिंग के लिए, आपका कार्य उसे मैच करने के लिए उपयुक्त पैटर्न लिखना है।
निर्देश
- निम्न के लिए पैटर्न लिखें:
xxx-xxx-xxxxफ़ॉर्मेट में एक टेलीफ़ोन नंबर। आप यह पिछले एक अभ्यास में कर चुके हैं।- इस फ़ॉर्मेट में एक स्ट्रिंग: एक डॉलर साइन, मनमानी संख्या में डिजिट्स, एक दशमलव बिंदु, 2 डिजिट्स।
- डॉलर साइन के लिए
\$, मनमानी संख्या में डिजिट्स के लिए\d*, दशमलव बिंदु के लिए\., औरxसंख्या में डिजिट्स के लिए\d{x}का उपयोग करें।
- डॉलर साइन के लिए
- एक कैपिटल लैटर, जिसके बाद मनमानी संख्या में अल्फ़ान्यूमेरिक कैरेक्टर्स हों।
- किसी भी कैपिटल लैटर के लिए
[A-Z]का उपयोग करें और उसके बाद मनमानी संख्या में अल्फ़ान्यूमेरिक कैरेक्टर्स के लिए\w*का उपयोग करें।
- किसी भी कैपिटल लैटर के लिए
import re
# Write the first pattern
pattern1 = bool(re.match(pattern='____', string='123-456-7890'))
print(pattern1)
# Write the second pattern
pattern2 = bool(re.match(pattern='____', string='$123.45'))
print(pattern2)
# Write the third pattern
pattern3 = bool(re.match(pattern='____', string='Australia'))
print(pattern3)
# Write the first pattern
pattern1 = bool(re.match(pattern='\d{3}-\d{3}-\d{4}', string='123-456-7890'))
print(pattern1)
# Write the second pattern
pattern2 = bool(re.match(pattern='\$\d*\.\d{2}', string='$123.45'))
print(pattern2)
# Write the third pattern
pattern3 = bool(re.match(pattern='[A-Z]\w*', string='Australia'))
print(pattern3)
Ex().test_correct(test_object('pattern1'), test_function('re.match', index=1))
Ex().test_function('print', index=1)
Ex().test_correct(test_object('pattern2'), test_function('re.match', index=2))
Ex().test_function('print', index=2)
Ex().test_correct(test_object('pattern3'), test_function('re.match', index=3))
Ex().test_function('print', index=3)
success_msg("Great work! You're mastering the fundamentals of writing regular expressions!")
- आपके रेगुलर एक्सप्रेशन को पहले पैटर्न के लिए तीन घटकों का मिलान करना है:
xxx,xxx, औरxxxx। पहले दो\d{3}से मैच होते हैं, जबकि आखिरी\d{4}से। इन प्रत्येक घटकों के बीच में-होना चाहिए।
यदि आप इस कोर्स से और सीखना चाहते हैं, तो यहाँ लिंक है।
DataCamp का Python String Tutorial देखें।