Computer/python

문자열에 괄호"(, )" 로 쌓인 글자 추출하기

Kyubotics 2018. 6. 5. 19:42
반응형

문자열에서 괄호 기호 "(, )"로 쌓여있는 글자를 추출하는 방법을 정리해 보았다.


예를 들어 입력된 문자열이 LOG_ADD(LOG_FLOAT, actuatorThrust, &actuatorThrust) 로 되어있고 

여기서 () 안의 내용만 뜯어내기 위해서는 다음과 같이 하면 된다.


먼저 re (Regular Expression) 모듈이 필요하다. 


import re
str ="LOG_ADD(LOG_FLOAT, actuatorThrust, &actuatorThrust)"
items = re.findall('\(([^)]+)', str)   #extracts string in bracket()
print items

이렇게 실행하면 다음과 같은 결과가 출력된다.


Output


['LOG_FLOAT, actuatorThrust, &actuatorThrust']


re 모듈의 보다 자세한 설명은 https://docs.python.org/2/howto/regex.html 참조.

반응형