목록Programming/Python (5)
MJay
The Wildcard Character The . (or dot) character in a regular expression is called a wildcard and will match any character except for a newline. For example, enter the following into the interactive shell: >>> atRegex = re.compile(r'.at') >>> atRegex.findall('The cat in the hat sat on the flat mat.') ['cat', 'hat', 'sat', 'lat', 'mat'] Remem..
Greedy and Nongreedy Matching Since (Ha){3,5} can match three, four, or five instances of Ha in the string 'HaHaHaHaHa', you may wonder why the Match object’s call to group() in the previous curly bracket example returns 'HaHaHaHaHa' instead of the shorter possibilities. After all, 'HaHaHa' and 'HaHaHaHa' are also valid matches of the regular expression (Ha){3,5}...
Introduction Regular expressions are huge time-savers, not just for software users but also for programmers. In fact, tech writer Cory Doctorow argues that even before teaching programming, we should be teaching regular expressions: “Knowing [regular expressions] can mean the difference between solving a problem in 3 steps and solving it in 3,000 steps. When you’re a nerd, you forget that the pr..
Python Basicsprint ('Alice' * 5) 'AliceAliceAliceAliceAlice'Flow Controlspam = ['cat', 'bat', 'rat', 'elephant'] spam[0:4] ['cat', 'bat', 'rat', 'elephant'] spam[1:3] ['bat', 'rat'] spam[0:-1] ['cat', 'bat', 'rat']Changing Values in a List with Indexesspam = ['cat', 'bat', 'rat', 'elephant'] spam[1] = 'aardvark' spam ['cat', 'aardvark', 'rat', 'elephant'] spam[2] = spam[1] spam ['cat', 'aardvark..