[Hindi]NLP 06# Regular Expressions |NLP|Python 3|Natural Language Processing|2019
Code:
# -*- coding: utf-8 -*-
"""NLP_Ex5.ipynb
Automatically generated by Colaboratory.
"""
text = "My phone number is 111-222-3333, call me soon!!!"
text
'phone' in text
'111-222-3333' in text
import re
pattern = 'phone'
re.search(pattern,text)
match = re.search(pattern,text)
match.span()
match.start()
match.end()
text = "My phone is a bran new phone"
match = re.search(pattern,text)
match
match.span()
all_match = re.findall("phone",text)
len(all_match)
for match in re.findall('phone',text):
print(match)
text = "My phone number is 666-555-8888"
pattern = r'\d\d\d-\d\d\d-\d\d\d\d'
ph = re.search(pattern,text)
ph
ph.group()
pattern = r'(\d{3})-(\d{3})-(\d{4})'
match = re.search(pattern,text)
match.group(3)
re.search(r"man|woman","This woman is my best friend")
re.findall(r".man","This man & woman are my best friends")
re.findall(r"\d$","THis ends with a number 3")
re.findall(r"^\d","8 is my fav. number")
phrase = "THere are 3 numbers 34 inside 5 this sentence"
re.findall(r"[^\d]+",phrase)
phrase2 = "This is a string! but it has puncuation. How to remove it?"
mylist = re.findall(r"[^!.? ]+",phrase2)
mylist
' '.join(mylist)
Recommended Book:
Click Here |
0 Comments