# Importera Python modulen för reguljära uttryck, se https://docs.python.org/3/library/re.html # för ytterligare information och instruktioner om modulen. import re # Här är en textsträng som vi använder i uppgiften. textstring = 'abc ab acc, add' # Uppgift 1, grupperingar med finditer(). def group_1(): """ Example of how to use the objects which is returned by finditer(). """ txt = 'Sommaren är verkligen snart här.' regexp = re.compile(r'[a-zåäö]+', re.U) matchlist = regexp.finditer(txt) for m in matchlist: a = m.group(0) print('a=', a) # Med grupperingar. def group_2(): """ Example of how to use the objects which is returned by finditer(). """ txt = 'Sommaren är verkligen snart här.' regexp = re.compile(r'([a-zåäö]+) ([a-zåäö]+)', re.I) matchlist = regexp.finditer(txt) for m in matchlist: a1 = m.group(0) a2 = m.groups() b = m.group(1) c = m.group(2) print('a1=', a1, 'a2=', a2, 'b=', b, 'c=',c) # Uppgift 2, substitutioner med regex. def subs(sub, sub_with): """ Example of how to use the sub function. """ sub1 = re.compile(sub, re.U) string = sub1.sub(sub_with, string) print(string) # Uppgift 3, girighet. def greedy(): """ Find from m to n of a regular expression. """ txt = 'Sommaren är verkligen snart här.' regexp = re.compile(r'([a-zåäö]*) ([a-zåäö]{4,})', re.I) matchlist = regexp.finditer(txt) for m in matchlist: a = m.group(0) b = m.group(1) c = m.group(2) print('a=', a, 'b=', b, 'c=',c) # Uppgift 4, använd match. def match_with_re(): """ Find all words which contains its exact half. """ txt = "Farmor och farfar bjöd på kaka innan vi åkte till mormor." regex = re.compile(r'\b(.+)(\1)\b', re.I) matchlst = regex.match(txt) for match in matchlst: print(match.group(0)) print(match.groups) def match_half(): """ Find all words which contains its exact half. """ txt = "Farmor och farfar bjöd på kaka innan vi åkte till mormor." regex = re.compile(r'\b(.+)(\1)\b', re.I) matchlst = regex.findall(txt) for match in matchlst: print(match) # Uppgift 5, använd search. def search_with_re(): txt = 'Här kan stoppa in vilken textsträng som helst och den kan vara hur lång som helst!' regex = re.compile(r'[a-zåäö]+') matchobj = regex.search(txt) print ('Första elementet som matchade vårt uttryck var: ', matchobj.group(0)) # Uppgift 6, flera subbar.# Importera Python modulen för reguljära uttryck, se https://docs.python.org/3/library/re.html # för ytterligare information och instruktioner om modulen. import re # Här är en textsträng som vi använder i uppgiften. textstring = 'abc ab acc, add' # Uppgift 1, grupperingar med finditer(). def group_1(): """ Example of how to use the objects which is returned by finditer(). """ txt = 'Sommaren är verkligen snart här.' regexp = re.compile(r'[a-zåäö]+', re.U) matchlist = regexp.finditer(txt) for m in matchlist: a = m.group(0) print('a=', a) # Med grupperingar. def group_2(): """ Example of how to use the objects which is returned by finditer(). """ txt = 'Sommaren är verkligen snart här.' regexp = re.compile(r'([a-zåäö]+) ([a-zåäö]+)', re.I) matchlist = regexp.finditer(txt) for m in matchlist: a1 = m.group(0) a2 = m.groups() b = m.group(1) c = m.group(2) print('a1=', a1, 'a2=', a2, 'b=', b, 'c=',c) # Uppgift 2, substitutioner med regex. def subs(sub, sub_with): """ Example of how to use the sub function. """ sub1 = re.compile(sub, re.U) string = sub1.sub(sub_with, string) print(string) # Uppgift 3, girighet. def greedy(): """ Find from m to n of a regular expression. """ txt = 'Sommaren är verkligen snart här.' regexp = re.compile(r'([a-zåäö]*) ([a-zåäö]{4,})', re.I) matchlist = regexp.finditer(txt) for m in matchlist: a = m.group(0) b = m.group(1) c = m.group(2) print('a=', a, 'b=', b, 'c=',c) # Uppgift 4, använd match. def match_with_re(): """ Find all words which contains its exact half. """ txt = "Farmor och farfar bjöd på kaka innan vi åkte till mormor." regex = re.compile(r'\b(.+)(\1)\b', re.I) matchlst = regex.match(txt) for match in matchlst: print(match.group(0)) print(match.groups) def match_half(): """ Find all words which contains its exact half. """ txt = "Farmor och farfar bjöd på kaka innan vi åkte till mormor." regex = re.compile(r'\b(.+)(\1)\b', re.I) matchlst = regex.findall(txt) for match in matchlst: print(match) # Uppgift 5, använd search. def search_with_re(): txt = 'Här kan stoppa in vilken textsträng som helst och den kan vara hur lång som helst!' regex = re.compile(r'[a-zåäö]+') matchobj = regex.search(txt) print ('Första elementet som matchade vårt uttryck var: ', matchobj.group(0)) # Uppgift 6, flera subbar. def n_subs(lst_tuples): """ Takes in a list of tuples. The first element of each tuple is a regexp and the second element in the tuple is what the matching regexp should be substitued with. Ex. n_subs([('(a|b)', 'C'), ('([,\.])', ' \\1')]) """ string = textstring for tup in lst_tuples: sub1 = re.compile(tup[0], re.U) string = sub1.sub(tup[1], string) #print (string) print (string) #n_subs([('(a|b)', 'C'), ('([,\.])', ' \\1')]) #group_1() #group_2() #search_with_re() greedy() def n_subs(lst_tuples): """ Takes in a list of tuples. The first element of each tuple is a regexp and the second element in the tuple is what the matching regexp should be substitued with. Ex. n_subs([('(a|b)', 'C'), ('([,\.])', ' \\1')]) """ string = textstring for tup in lst_tuples: sub1 = re.compile(tup[0], re.U) string = sub1.sub(tup[1], string) #print (string) print (string) #n_subs([('(a|b)', 'C'), ('([,\.])', ' \\1')]) #group_1() #group_2() #search_with_re() greedy()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question