Allen's 데이터 맛집

[0] Intro. Java 정규식 단어 추출 본문

etc Project/간단한 자연어 데이터 처리

[0] Intro. Java 정규식 단어 추출

Allen93 2024. 7. 30. 15:14

프로젝트 개요

Java 정규식을 활용하여 단어를 추출하는 프로젝트를 소개합니다. 이 프로젝트는 제가 학부시절 소소하게 해보았었고, Java를 사용해 텍스트에서 특정 단어를 추출해 보았습니다.

 

기본 사용법

정규식(Regex)은 텍스트를 검색하고 조작하는 데 매우 강력한 도구입니다. 프로젝트의 기본 사용법을 통해 간단한 단어 추출 예제를 살펴보겠습니다.

 
import java.util.regex.*;
import java.util.ArrayList;

public class BasicWordExtraction {
    public static void main(String[] args) {
        String text = "Hello, this is a sample text with several words.";
        Pattern pattern = Pattern.compile("\\b\\w+\\b");
        Matcher matcher = pattern.matcher(text);
        
        ArrayList<String> words = new ArrayList<>();
        while (matcher.find()) {
            words.add(matcher.group());
        }
        
        System.out.println("Extracted words: " + words);
    }
}

 

이 예제는 텍스트에서 단어를 추출하는 기본적인 방법을 보여줍니다. 정규식 \\b\\w+\\b는 단어 경계를 의미하며, 텍스트에서 단어를 찾습니다.

 

https://github.com/siilver94/java-regex-word-extraction

 

GitHub - siilver94/java-regex-word-extraction

Contribute to siilver94/java-regex-word-extraction development by creating an account on GitHub.

github.com

 

728x90