正規表現、メタ文字の使い方(python)



Python
Pythonとは
基本的な使い方
IDLE
Jupyter Notebook
Markdown
コマンドプロンプトで実行
仮想環境の構築
仮想環境でIDLEを実行
ライブラリのインストール
pipの使い方
numpy , matplotlib等
graphviz
pytorch
Mecab
Pythonの関数:一覧
共通関数
append , extend
class
copy
csv.reader
csv.writer
def , return
dict , defaultdict
enumerate
exit
for
if
import
in
input
lambda
len
list
min/max
OrderedDict
open/close
os
pickle
print
range
re.split
read/readline
round/floor/ceil
split
sys.argv
time
while
write
zip
・特殊メソッド
 ・__name__
 ・__iter__ , __next__
正規表現、メタ文字
データの型の種類
四則演算 (+ , - , * , /)
コメントアウト (# , ''')
numpy
append
arange
argmax/argmin
array
asfarray
astype , dtype
digitize
dot
hstack/vstack
linalg.solve
linspace
mean
meshgrid
mgrid
ndim
ndmin
pad
poly1d
polyfit
prod
random
reshape
savetxt/loadtxt
shape
std
transpose
where
zeros/zeros_like
scipy
expit
imread
interpolate
matplotlib
imshow
figure
pcolormesh
plot
scatter
scikit-learn
GaussianNB
KMeans
KNeighborsClassifier
SVC
tree
keras
chainer
chainerrl
pytorch
BCELoss , MSELoss
Embedding
device
Sequential
Dataset, Dataloader
RNN, LSTM
OpenAI gym
Blackjack-v0
CartPole-v0
tkinter
frame, grid
画像表示
画像を切り取り表示
画像を保存
目的別
ステップ関数
1 of K 符号化法
線形補間
配列に番号をつける

公開日:2021/9/5          

■説明
正規表現とは文字列のパターンを表現する記述方法全般のことです。メタ文字とは正規表現を取得するための関数を少ない文字列で表現したもので、以下例があります。

表現説明
.任意の1文字
\d任意の数字
+1回以上繰り返す
{m,n}m〜n回の繰り返し
^文字列の先頭
$文字列の末尾
*直前の文字を繰り返す
{n}直前のパターンのn回の繰り返し
?0回または1回の繰り返し


■具体例

import re

text = '月火水木12345'

word = re.findall('12.4', text) # 2と4の間の文字を取得
print(word)

    → ['1234']

word = re.findall('123.*', text) # 123の後の文字を全て取得
print(word)

    → ['12345']

word = re.findall('^123.*', text) # 123が先頭にある場合のみ、文字列をすべて取得
print(word)

    → []

word = re.findall('\d', text) # 数字をすべて取得
print(word)

    → ['1', '2', '3', '4', '5']


?の使い方

import re

text = '1みかん2いちご1ぶどう2りんご'

word = re.findall('1.+?2', text) # 1と2の間の文字を取得
print(word)

    → ['1みかん2', '1ぶどう2']

word = re.findall('1.+2', text) # ?がないと、最後の2を取得する
print(word)

    → ['1みかん2いちご1ぶどう2']










サブチャンネルあります。⇒ 何かのお役に立てればと

関連記事一覧



Python
Pythonとは
基本的な使い方
IDLE
Jupyter Notebook
Markdown
コマンドプロンプトで実行
仮想環境の構築
仮想環境でIDLEを実行
ライブラリのインストール
pipの使い方
numpy , matplotlib等
graphviz
pytorch
Mecab
Pythonの関数:一覧
共通関数
append , extend
class
copy
csv.reader
csv.writer
def , return
dict , defaultdict
enumerate
exit
for
if
import
in
input
lambda
len
list
min/max
OrderedDict
open/close
os
pickle
print
range
re.split
read/readline
round/floor/ceil
split
sys.argv
time
while
write
zip
・特殊メソッド
 ・__name__
 ・__iter__ , __next__
正規表現、メタ文字
データの型の種類
四則演算 (+ , - , * , /)
コメントアウト (# , ''')
numpy
append
arange
argmax/argmin
array
asfarray
astype , dtype
digitize
dot
hstack/vstack
linalg.solve
linspace
mean
meshgrid
mgrid
ndim
ndmin
pad
poly1d
polyfit
prod
random
reshape
savetxt/loadtxt
shape
std
transpose
where
zeros/zeros_like
scipy
expit
imread
interpolate
matplotlib
imshow
figure
pcolormesh
plot
scatter
scikit-learn
GaussianNB
KMeans
KNeighborsClassifier
SVC
tree
keras
chainer
chainerrl
pytorch
BCELoss , MSELoss
Embedding
device
Sequential
Dataset, Dataloader
RNN, LSTM
OpenAI gym
Blackjack-v0
CartPole-v0
tkinter
frame, grid
画像表示
画像を切り取り表示
画像を保存
目的別
ステップ関数
1 of K 符号化法
線形補間
配列に番号をつける