python正则表达式re模块详解
快速入门
import re pattern = 'this' text = 'Does this text match the pattern?' match = re.search(pattern, text) s = match.start() e = match.end() print('Found "{0}"\nin "{1}"'.format(match.re.pattern, match.string)) print('from {0} to {1} ("{2}")'.format( s, e, text[s:e]))
执行结果:
#python re_simple_match.py Found "this" in "Does this text match the pattern?" from 5 to 9 ("this") import re # Precompile the patterns regexes = [ re.compile(p) for p in ('this', 'that')] text = 'Does this text match the pattern?' print('Text: {0}\n'.format(text)) for regex in regexes: if regex.search(text): result = 'match!' else: result = 'no match!' print('Seeking "{0}" -> {1}'.format(regex.pattern, result))
执行结果:
#python re_simple_compiled.py Text: Does this text match the pattern? Seeking "this" -> match! Seeking "that" -> no match! import re text = 'abbaaabbbbaaaaa' pattern = 'ab' for match in re.findall(pattern, text): print('Found "{0}"'.format(match))
执行结果:
#python re_findall.py Found "ab" Found "ab" import re text = 'abbaaabbbbaaaaa' pattern = 'ab' for match in re.finditer(pattern, text): s = match.start() e = match.end() print('Found "{0}" at {1}:{2}'.format(text[s:e], s, e))
执行结果:
#python re_finditer.py Found "ab" at 0:2 Found "ab" at 5:7

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











多くのウェブサイト開発者は、ランプアーキテクチャの下でnode.jsまたはPythonサービスを統合する問題に直面しています:既存のランプ(Linux Apache MySQL PHP)アーキテクチャWebサイトのニーズ...

Scapy Crawlerを使用する場合、パイプラインの永続的なストレージファイルを書くことができない理由は?ディスカッションデータクローラーにScapy Crawlerを使用することを学ぶとき、あなたはしばしば...

Python Process Poolは、クライアントが立ち往生する原因となる同時TCP要求を処理します。ネットワークプログラミングにPythonを使用する場合、同時のTCP要求を効率的に処理することが重要です。 ...

python functools.partialオブジェクトのpython functools.partialを使用してPythonを使用する視聴方法を深く探索します。

Pythonクロスプラットフォームデスクトップアプリケーション開発ライブラリの選択多くのPython開発者は、WindowsシステムとLinuxシステムの両方で実行できるデスクトップアプリケーションを開発したいと考えています...

Python:Hourglassグラフィック図面と入力検証この記事では、Python NoviceがHourglass Graphic Drawingプログラムで遭遇する可変定義の問題を解決します。コード...

データの変換と統計:大規模なデータセットの効率的な処理この記事では、製品情報を含むデータリストを別の含有しているものに変換する方法を詳細に紹介します...

LinuxターミナルでPythonバージョンを表示する際の許可の問題の解決策PythonターミナルでPythonバージョンを表示しようとするとき、Pythonを入力してください...
