[ENG] STEEM Security update - added power down alert

by joviansummer original STEEMIT post: https://steemit.com/blog/@joviansummer/eng-steem-security-update-added-power-down-alert STEEM Security - receive STEEM alert messages via Discord Hello, this is @joviansummer(witness: @jswit). STEEM Security code has been revised. Previously, STEEM Security was sending alert message via discord for the following 3 kinds of operations: sending STEEM/SBD to another account changing SP delegation account update Now, power down operation has been added. If there is a power down operation, you will also receive alert message. Please check the original post of the above link for detailed information on how to use the service. Thank you for reading, and have a wonderful day! @joviansummer's STEEM projects @jswit witness: I'm running a STEEM witness node. I'd really appreciate it if you vote for my witness account @jswit. ( https://steemitwallet.com/~witnesses ) [ENG] Introducing @jswit witness project @jsup curation: [ENG] Int...

파이썬에서 정규표현식(regex)을 이용한 문자열 검색

by joviansummer
original STEEMIT post: https://steemit.com/blog/@joviansummer/regex


파이썬에서 정규표현식(regular expression)을 이용해서 검색 조건에 일치하는 문자열인지 확인하는 방법입니다.

예시를 보겠습니다. 문자열 'abc', 'def', '123', '1abc' 이렇게 4개를 묶어 리스트(list)로 만들고 영문 소문자([a-z])를 검색해 봅니다.

import re

# 리스트 생성
str_list = ['abc', 'def', '123', '1abc']

# 적용할 정규표현식을 컴파일하여 regex에 할당
regex = re.compile('[a-z]')

이제 for 반목문으로 str_list에 속한 문자열들에 대해서 검색을 합니다. 우선 match()를 사용해 봅니다.

for i in str_list:
  if regex.match(i):
    print(i)

abc def

match()는 문자열 처음부터 조건과 일치하는지 검색합니다. 위의 예시에서는 문자열 'abc'와 'def'가 처음부터 영문 소문자로 시작하므로 조건에 부합하여 줄력되었습니다.

search()는 문자열 전체에 대해 일치하는 부분이 있는지를 검색합니다.

for i in str_list:
  if regex.search(i):
    print(i)

abc def 1abc

문자열 '1abc'의 경우 영문 소문자로 시작하지 않기 때문에 match()에서는 조건에 부합하지 않지만, 영문 소문자를 포함하고 있으므로 search()에서는 조건에 부합되어 출력됩니다.

findall()은 조건에 맞는 문자열을 모두 찾아서 리스트 형태로 가져옵니다.

x = 'hello'
y = regex.findall(x)
print(y)

['h', 'e', 'l', 'l', 'o']

문장으로부터 영문 소문자 단어를 분리하려면 아래와 같이 해 볼 수 있겠습니다.

# 영문 소문자가 1번 이상 반복: 정규표현식 [a-z]+
regex2 = re.compile('[a-z]+')
x = 'my name is joviansummer.'
y = regex2.findall(x)
print(y)

['my', 'name', 'is', 'joviansummer']

Comments

Popular posts from this blog

[ENG] jsup 2.0 - make your upvote great again

jsup/avle 보팅 코드 갱신 - 1일1포스팅 강박에서 벗어나기

Nuitka - 파이썬 스크립트를 바이너리 실행 파일로 변환