java

java 정규식 문자열 추출

kimbs0301 2024. 7. 4. 14:03

java 정규식 사용해서 텍스트 파일 문자열 추출

 

정규식 패턴에 사용한 특수 문자

더보기

#{...}
"..."

텍스트 파일 내부 문자열 추출

 

 

file1.txt

더보기

User.class : ID=#{id}, EMAIL=#{email}
Account.class : AID=#{id}, EMAIL=#{email}
Member.class : MID=#{id}, EMAIL=#{email}

import java.io.FileInputStream;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Regex1 {

	public static void main(String[] args) throws Exception {
		String regexp = "#\\{(\\w+)\\}";
        
		Pattern pattern = Pattern.compile(regexp);
		try {
			FileInputStream fis = new FileInputStream("C:/workspace/java21/regex/file1.txt");
			Scanner s = new Scanner(fis);
			while (s.hasNext()) {
				String text = s.nextLine();
				System.out.println(text);
				Matcher matcher = pattern.matcher(text);
				while (matcher.find()) {
					System.out.println("Start index:" + matcher.start() + " End index:" + matcher.end());
					String value = matcher.group(1);
					System.out.println(value);
				}
				System.out.println();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

file2.txt

더보기

User.class : ID=1, EMAIL="email1 44"1
Account.class : AID=2, EMAIL="email2 55"2
Member.class : MID=3, EMAIL="email3 66"3

import java.io.FileInputStream;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Regex2 {

	public static void main(String[] args) throws Exception {
		String regexp = "\"(.+)\"";

		Pattern pattern = Pattern.compile(regexp);
		try {
			FileInputStream fis = new FileInputStream("C:/workspace/java21/regex/file2.txt");
			Scanner s = new Scanner(fis);
			while (s.hasNext()) {
				String text = s.nextLine();
				System.out.println(text);
				Matcher matcher = pattern.matcher(text);
				while (matcher.find()) {
					System.out.println("Start index:" + matcher.start() + " End index:" + matcher.end());
					String value = matcher.group(1);
					System.out.println(value);
				}
				System.out.println();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

file3.txt

더보기

User.class : ID="1", EMAIL="email1 44"1
Account.class : AID="2", EMAIL="email2 55"2
Member.class : MID="3", EMAIL="email3 66"3

import java.io.FileInputStream;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Regex3 {

	public static void main(String args[]) throws Exception {
		String regexp = "\\\"([^)\\\"(]+)\\\"";

		Pattern pattern = Pattern.compile(regexp);
		try {
			FileInputStream fis = new FileInputStream("C:/workspace/java21/regex/file3.txt");
			Scanner s = new Scanner(fis);
			while (s.hasNext()) {
				String text = s.nextLine();
				System.out.println(text);
				Matcher matcher = pattern.matcher(text);
				while (matcher.find()) {
					System.out.println("Start index:" + matcher.start() + " End index:" + matcher.end());
					String value = matcher.group(1);
					System.out.println(value);
				}
				System.out.println();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

'java' 카테고리의 다른 글

java 8byte guid 생성  (0) 2024.07.04
java Virtual Thread Producer Consumer 패턴  (0) 2024.07.04
java LittleEndian byte array 정수값 변환  (0) 2024.07.04
java BigDecimal 지수 표현  (0) 2024.07.04
java assert 키워드  (0) 2024.07.04