No.036 [Python] 정규 표현식 "모듈 re"
5789 단어 Pythonprogramming
이번에는 정규 표현식'모듈re'를 쓰겠습니다.
I'll write about "re module", regular expression in python"on this page.
■ 문자열의 시작이 패턴과 일치하는지 여부: match () 함수
The judgement of agreement between a lead position of strings and a pattern: match()function
>>> import re
>>>
>>> w = "one two one two"
>>>
>>> # 文字列の先頭とパターンとの一致有無の確認:match()
>>>
>>> # re.match()にて調べることが可能
>>>
>>> m = re.match("one",w)
>>>
>>> print(m)
<re.Match object; span=(0, 3), match='one'>
>>> # ↑ 一致の場合は、matchオブジェクトを返す
>>> # matchオブジェクトは、以下のmethodを持つ
>>> # group(), start(), end(), span(), etc.
>>>
>>> import re
>>>
>>> w = "one two one two"
>>>
>>> m = re.match("one",w)
>>> print(m)
<re.Match object; span=(0, 3), match='one'>
>>>
>>> print(m.group())
one
>>>
>>> print(m.start())
0
>>>
>>> print(m.end())
3
>>>
>>> print(m.span())
(0, 3)
>>> # group():パターンに一致した全体を返す
>>> # groups():()で囲まれた部分と一致した各文字列をタプルで取得可能
>>>
>>> import re
>>> w = "one two one two"
>>>
>>> m = re.match("(one) (two)",w)
>>>
>>> print(m)
<re.Match object; span=(0, 7), match='one two'>
>>>
>>> print(m.group())
one two
>>>
>>> print(m.groups())
('one', 'two')
>>>
>>> # 先頭に一致する文字列がない場合:Noneを返す
>>>
>>> m = re.match("two", w)
>>>
>>> print(m)
None
■ 조사 모델의 일치: 검색 ()
Search for pattern agreements
>>> # 先頭にない文字列を調べることが可能
>>> # re.match()と同様、一致の場合はmatchオブジェクトを返す
>>>
>>> import re
>>> w = "one two one two"
>>>
>>> m = re.search("one",w)
>>>
>>> print(m)
<re.Match object; span=(0, 3), match='one'>
>>>
>>> m = re.search("two",w)
>>>
>>> print(m)
<re.Match object; span=(4, 7), match='two'>
>>> #↑ 文字列中に一致箇所が複数あっても最初に一致したかのみ返す
■ 모든 일치점을 목록으로 반환:findall ()
Return all the part of agreement by lists
>>> # 一致箇所を全てリストにして返す
>>> # 返すのはmatchオブジェクトではない
>>>
>>> import re
>>>
>>> w = "one two one two"
>>>
>>> m = re.findall("one",w)
>>>
>>> print(m)
['one', 'one']
>>>
>>> m = re.findall("one two",w)
>>>
>>> print(m)
['one two', 'one two']
■ 일치하는 곳 모두 균형기로 돌려주기:finditer()
Return all the part of agreement by iterators
>>> # re.finditer():一致箇所をmatchオブジェクトのイテレータで返す
>>> # re.findallとは異なり、matchオブジェクトを得られる
>>>
>>>
>>> import re
>>> w = "one two one two"
>>>
>>> m = re.finditer('one', w)
>>>
>>> print(m)
<callable_iterator object at 0x107b79fd0>
>>>
>>> for match in m:
print(match)
<re.Match object; span=(0, 3), match='one'>
<re.Match object; span=(8, 11), match='one'>
■ 일치된 교체:sub()/subn()
Replacement the part of agreement
>>> import re
>>> w = "one two one two"
>>>
>>> m = re.sub("one", "ONE",w)
>>>
>>> print(m)
ONE two ONE two
>>>
>>> m = re.sub("one two", "ONE TWO", w)
>>>
>>> print(m)
ONE TWO ONE TWO
>>>
>>> #パターンの一部を囲み、置換後の文字列中の一致箇所を使用することが可能
>>>
>>> m = re.sub("(one) (two)", "\\1X\\2",w)
>>>
>>> print(m)
oneXtwo oneXtwo
>>>
>>> m = re.sub('(one) (two)', r'\1X\2', w)
>>>
>>> print(m)
oneXtwo oneXtwo
■ 패턴에 따른 문자열 분할: split ()
String division by patterns
>>> import re
>>> w = "one two one two"
>>> m = re.split(" ", w)
>>>
>>> print(m)
['one', 'two', 'one', 'two']
■ 정규 표현식 대상의 컴파일:comple ()
Complie of regular expression obejcts
>>> # re.compile():同じパターンの繰り返し使用の場合
>>> # パターンをコンパイルして正規表現オブジェクトを生成したほうがいい
>>>
>>> import re
>>> w = "one two one two"
>>>
>>> com = re.compile("one")
>>>
>>> m = com.match(w)
>>> print(m)
<re.Match object; span=(0, 3), match='one'>
>>>
>>> m = com.findall(w)
>>> print(m)
['one', 'one']
>>>
>>> m = com.sub("ONE", w)
>>> print(m)
ONE two ONE two
수시로 업데이트되므로 정기적으로 구독해주세요.I'll update my article at all times.
So, please subscribe my articles from now on.
본 보도에 관하여 만약 무슨 요구가 있으면 마음대로 메시지를 남겨 주십시오!
If you have some requests, please leave some messages! by You-Tarin
또한 Qita에 투고한 내용은 언제든지 블로그에 가고 싶으니 잘 부탁드립니다.
Reference
이 문제에 관하여(No.036 [Python] 정규 표현식 "모듈 re"), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/You-Tarin/items/e0b0a0c33890e78d30ad텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)