jsup 2.1 - investor's relief (experiment on separation of investment and SNS)

by joviansummer original STEEMIT post: https://steemit.com/blog/@joviansummer/jsup-2-1-investor-s-relief-experiment-on-separation-of-investment-and-sns Hello, this is @joviansummer(witness: @jswit). I'm starting a new experiment regarding my @jsup vote service. The purpose of this experiment is to separate STEEM investment and social interaction(posting/commenting). For many people, writing a post daily(literally 365 posts per year) is not easy. It's not fun, and mandatory daily post only written for self-voting delegation service tends to result in useless low-quality content. I hope this new function of @jsup will reduce those posts, and also make it a bit more easy to invest in STEEM without worrying about daily posting. I think pressure of daily posting significantly undermines Steem as SNS. It's not fun. This is how it works: From 11:45 PM to 11:50 PM every night(reference time zone is GMT+9), @jsup checks if eligible delegators wrote something and received daily v...

파이썬에서 정규표현식(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

스티미언의 영향력 지수 계산

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

jsup/avle code update - you don't have to write post everyday