001package org.biojava.bio.program.tagvalue; 002 003import java.util.ArrayList; 004import java.util.List; 005import java.util.regex.Matcher; 006import java.util.regex.Pattern; 007 008/** 009 * <p> 010 * A ValueChanger.Splitter that splits a line of text using a regular 011 * expression, returning one value per match. 012 * </p> 013 * 014 * <p> 015 * A list of values is generated by effectively executing: 016 * <pre> 017 * matcher = pattern.matcher(value.toString()); 018 * 019 * while(matcher.find()) { 020 * values.add(matcher.group(matchGroup); 021 * } 022 * </pre> 023 * </p> 024 * 025 * @author Matthew Pocock 026 * @since 1.3 027 */ 028public class RegexSplitter 029 implements 030 ChangeTable.Splitter 031{ 032 private Pattern pattern; 033 private int matchGroup; 034 035 /** 036 * Create a new RegexSplitter with a pattern. 037 * 038 * @param pattern the Pattern used to split values 039 * @param matchGroup the group to pull out - use 0 to pull out the whole match 040 */ 041 public RegexSplitter(Pattern pattern, int matchGroup) { 042 this.pattern = pattern; 043 this.matchGroup = matchGroup; 044 } 045 046 public List split(Object value) { 047 Matcher matcher = pattern.matcher(value.toString()); 048 049 List result = new ArrayList(); 050 while(matcher.find()) { 051 result.add(matcher.group(matchGroup)); 052 } 053 054 return result; 055 } 056} 057