Regex로 특정 단어부터 empty lines까지 찾는 법

조회수 247회

아래와 같은 문단에서 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./아니, 아빠야.
  • 엽토군님 편집 감사합니다. 정토드 2023.1.31 09:18

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))
    

    실행하기 눌러서 해 보시면 될 거에요

    • 저도 딱 이렇게 해드리는 중에 답변올라와서 지웠어요 ㅋㅋㅋ 해결! 어드미니 2023.1.31 10:02

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)