파이썬 시작: Regular Expressions
Code ## 1. Regular expressions ##
strings = ["data science", "big data", "metadata"]
regex = "data"
## 2. Special characters ##
strings = ["bat", "robotics", "megabyte"]
regex = "b.t"
## 3. Beginnings and ends of string ##
strings = ["better not put too much", "butter in the", "batter"]
bad_string = "We also wouldn't want it to be bitter"
regex = "^b.tter"
## 5. Reading and printing the dataset ##
import csv
posts_with_header = csv.reader(open("askreddit_2015.csv", "r"))
posts = list(posts_with_header)[1:]
for i in range(0, 10):
print(posts[i])
## 6. Testing for matches ##
import re
of_reddit_count = 0
for post in posts:
post_string = post[0]
if re.search("of Reddit", post_string):
of_reddit_count = of_reddit_count + 1
## 7. Accounting for inconsistencies ##
import re
of_reddit_count = 0
for row in posts:
if re.search("of [Rr]eddit", row[0]) != None:
of_reddit_count += 1
## 8. Escaping special characters ##
import re
serious_count = 0
for row in posts:
if re.search("\[Serious\]", row[0]) != None:
serious_count += 1
## 9. Refining the search ##
import re
serious_count = 0
for row in posts:
if re.search("\[[Ss]erious\]", row[0]) != None:
serious_count += 1
## 10. More inconsistency ##
import re
serious_count = 0
for row in posts:
if re.search("[\[\(][Ss]erious[\]\)]", row[0]) != None:
serious_count += 1
## 11. Multiple regular expressions ##
import re
serious_start_count = 0
serious_end_count = 0
serious_count_final = 0
for post in posts:
post_string = post[0]
if re.search("^[\[\(][Ss]erious[\]\)]", post_string) != None:
serious_start_count += 1
if re.search("[\[\(][Ss]erious[\]\)]$", post_string) != None:
serious_end_count += 1
if re.search("^[\[\(][Ss]erious[\]\)]|[\[\(][Ss]erious[\]\)]$", post_string) != None:
serious_count_final += 1
## 12. Substituting strings ##
import re
posts_new = []
for row in posts:
row[0] = re.sub("[\[\(][Ss]erious[\]\)]", "[Serious]", row[0])
posts_new.append(row)
## 13. Matching years ##
import re
year_strings = []
for string in strings:
if re.search("[1-2][0-9][0-9][0-9]", string) != None:
year_strings.append(string)
## 14. Repeating regular expressions ##
import re
year_strings = []
for string in strings:
if re.search("[1-2][0-9]{3}", string) != None:
year_strings.append(string)
## 15. Extracting years ##
import re
years = re.findall("[1-2][0-9]{3}", years_string)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSON
JSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다.
그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다.
저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.
## 1. Regular expressions ##
strings = ["data science", "big data", "metadata"]
regex = "data"
## 2. Special characters ##
strings = ["bat", "robotics", "megabyte"]
regex = "b.t"
## 3. Beginnings and ends of string ##
strings = ["better not put too much", "butter in the", "batter"]
bad_string = "We also wouldn't want it to be bitter"
regex = "^b.tter"
## 5. Reading and printing the dataset ##
import csv
posts_with_header = csv.reader(open("askreddit_2015.csv", "r"))
posts = list(posts_with_header)[1:]
for i in range(0, 10):
print(posts[i])
## 6. Testing for matches ##
import re
of_reddit_count = 0
for post in posts:
post_string = post[0]
if re.search("of Reddit", post_string):
of_reddit_count = of_reddit_count + 1
## 7. Accounting for inconsistencies ##
import re
of_reddit_count = 0
for row in posts:
if re.search("of [Rr]eddit", row[0]) != None:
of_reddit_count += 1
## 8. Escaping special characters ##
import re
serious_count = 0
for row in posts:
if re.search("\[Serious\]", row[0]) != None:
serious_count += 1
## 9. Refining the search ##
import re
serious_count = 0
for row in posts:
if re.search("\[[Ss]erious\]", row[0]) != None:
serious_count += 1
## 10. More inconsistency ##
import re
serious_count = 0
for row in posts:
if re.search("[\[\(][Ss]erious[\]\)]", row[0]) != None:
serious_count += 1
## 11. Multiple regular expressions ##
import re
serious_start_count = 0
serious_end_count = 0
serious_count_final = 0
for post in posts:
post_string = post[0]
if re.search("^[\[\(][Ss]erious[\]\)]", post_string) != None:
serious_start_count += 1
if re.search("[\[\(][Ss]erious[\]\)]$", post_string) != None:
serious_end_count += 1
if re.search("^[\[\(][Ss]erious[\]\)]|[\[\(][Ss]erious[\]\)]$", post_string) != None:
serious_count_final += 1
## 12. Substituting strings ##
import re
posts_new = []
for row in posts:
row[0] = re.sub("[\[\(][Ss]erious[\]\)]", "[Serious]", row[0])
posts_new.append(row)
## 13. Matching years ##
import re
year_strings = []
for string in strings:
if re.search("[1-2][0-9][0-9][0-9]", string) != None:
year_strings.append(string)
## 14. Repeating regular expressions ##
import re
year_strings = []
for string in strings:
if re.search("[1-2][0-9]{3}", string) != None:
year_strings.append(string)
## 15. Extracting years ##
import re
years = re.findall("[1-2][0-9]{3}", years_string)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.