mirror of
https://github.com/apachecn/ailearning.git
synced 2026-05-03 10:01:56 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
import re
|
||||
|
||||
```
|
||||
@@ -17,7 +17,7 @@ import re
|
||||
|
||||
在 `re` 模块中, `re.match` 和 `re.search` 是常用的两个方法:
|
||||
|
||||
```
|
||||
```py
|
||||
re.match(pattern, string[, flags])
|
||||
re.search(pattern, string[, flags])
|
||||
```
|
||||
@@ -74,7 +74,7 @@ re.search(pattern, string[, flags])
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
string = 'hello world'
|
||||
pattern = 'hello (\w+)'
|
||||
|
||||
@@ -83,7 +83,7 @@ print match
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
<_sre.SRE_Match object at 0x0000000003A5DA80>
|
||||
|
||||
```
|
||||
@@ -92,26 +92,26 @@ print match
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
if match is not None:
|
||||
print match.group(0)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
hello world
|
||||
|
||||
```
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
if match is not None:
|
||||
print match.group(1)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
world
|
||||
|
||||
```
|
||||
@@ -120,7 +120,7 @@ world
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
string = 'hello there'
|
||||
pattern = 'hello (\w+)'
|
||||
|
||||
@@ -131,7 +131,7 @@ if match is not None:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
hello there
|
||||
there
|
||||
|
||||
@@ -143,7 +143,7 @@ there
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
pattern1 = re.compile('hello (\w+)')
|
||||
|
||||
match = pattern1.match(string)
|
||||
@@ -152,7 +152,7 @@ if match is not None:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
there
|
||||
|
||||
```
|
||||
@@ -163,13 +163,13 @@ there
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
pattern = '\\'
|
||||
print pattern
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
\
|
||||
|
||||
```
|
||||
@@ -178,14 +178,14 @@ print pattern
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
pattern = '\\\\'
|
||||
path = "C:\\foo\\bar\\baz.txt"
|
||||
print re.split(pattern, path)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
['C:', 'foo', 'bar', 'baz.txt']
|
||||
|
||||
```
|
||||
@@ -194,14 +194,14 @@ print re.split(pattern, path)
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
pattern = r'\\'
|
||||
path = r"C:\foo\bar\baz.txt"
|
||||
print re.split(pattern, path)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
['C:', 'foo', 'bar', 'baz.txt']
|
||||
|
||||
```
|
||||
@@ -212,7 +212,7 @@ print re.split(pattern, path)
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
%%file test.dat
|
||||
1312 foo
|
||||
1534 bar
|
||||
@@ -220,12 +220,12 @@ In [10]:
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Writing test.dat
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
fromregex(file, pattern, dtype)
|
||||
```
|
||||
|
||||
@@ -233,7 +233,7 @@ fromregex(file, pattern, dtype)
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
pattern = "(\d+)\s+(...)"
|
||||
dt = [('num', 'int64'), ('key', 'S3')]
|
||||
|
||||
@@ -243,7 +243,7 @@ print output
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
[(1312L, 'foo') (1534L, 'bar') (444L, 'qux')]
|
||||
|
||||
```
|
||||
@@ -252,19 +252,19 @@ print output
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
print output['num']
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
[1312 1534 444]
|
||||
|
||||
```
|
||||
|
||||
In [13]:
|
||||
|
||||
```
|
||||
```py
|
||||
import os
|
||||
os.remove('test.dat')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user