Regex로 특정 단어부터 empty lines까지 찾는 법
조회수 92회
아래와 같은 문단에서 002로 시작하는 문단을 찾으려면 어떻게 해야 하나요?
001 Hide and Seek/숨바꼭질 Can you see us?/우리가 보이니? Can you see me?/내가 보이니? Yes, I can see you./응, 보여. Can you see me?/내가 보이니? Yes, we can see you./응, 보여. Can you see me?/내가 보이니? Yes, we can see you./응, 보여. We can all see Dad./우리 모두 아빠가 보여요.
002 Reds and Blues/홍팀과 청팀 We are all in red./우리는 모두 빨간색으로 입었어요. We are all in blue./우리는 모두 파란색으로 입었어요. Come on the reds!/홍팀 화이팅! Come on the blues!/청팀 화이팅! Who is in red?/누가 빨간색으로 입은거지? Who is in blue?/누가 파란색으로 입은거지? We are all muddy./우리 모두 진흙투성이네요.
003 Big Feet/큰 발 Come and look at this./와서 이것 좀 봐. Come and look at this./와서 이것 좀 봐. Is it a big monster?/이거 큰 괴물이야? Come and look at this./와서 이것 좀 봐. Is it a big dinosaur?/이거 큰 공룡이야? Come and look at this./와서 이것 좀 봐. Is it a big giant?/이거 큰 거인이야? No, it is Dad./아니, 아빠야.
1 답변
-
import re # Define the regular expression pattern for entity-label pairs str = """001 Hide and Seek/숨바꼭질 Can you see us?/우리가 보이니? Can you see me?/내가 보이니? Yes, I can see you./응, 보여. Can you see me?/내가 보이니? Yes, we can see you./응, 보여. Can you see me?/내가 보이니? Yes, we can see you./응, 보여. We can all see Dad./우리 모두 아빠가 보여요. 002 Reds and Blues/홍팀과 청팀 We are all in red./우리는 모두 빨간색으로 입었어요. We are all in blue./우리는 모두 파란색으로 입었어요. Come on the reds!/홍팀 화이팅! Come on the blues!/청팀 화이팅! Who is in red?/누가 빨간색으로 입은거지? Who is in blue?/누가 파란색으로 입은거지? We are all muddy./우리 모두 진흙투성이네요. 003 Big Feet/큰 발 Come and look at this./와서 이것 좀 봐. Come and look at this./와서 이것 좀 봐. Is it a big monster?/이거 큰 괴물이야? Come and look at this./와서 이것 좀 봐. Is it a big dinosaur?/이거 큰 공룡이야? Come and look at this./와서 이것 좀 봐. Is it a big giant?/이거 큰 거인이야? No, it is Dad./아니, 아빠야. """ match = re.search(r'^002.*\n',str,re.MULTILINE) print(match.group(0))
실행하기 눌러서 해 보시면 될 거에요
댓글 입력