「Python」正規表現式で文字を否定で指定するサンプル
環境
PyCharm 2021.3.3
Python 3.9.2
書式
正規表現式 [^B]
「B」以外の文字を指定しています。
groupメソッドは一致した文字列を返します。
使用例
# coding: utf-8
import re
str = "[^B]"
res = re.search(str,'STUDY')
if res:
print(res.group(0))
else:
print(res)
# coding: utf-8
import re
str = "[^B]"
res = re.search(str,'STUDY')
if res:
print(res.group(0))
else:
print(res)
# coding: utf-8 import re str = "[^B]" res = re.search(str,'STUDY') if res: print(res.group(0)) else: print(res)
実行結果
S