cz.jalasoft.util.text.RegexFragment Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of JalasoftUtils Show documentation
Show all versions of JalasoftUtils Show documentation
A collection of utility classes that might be useful.
The newest version!
package cz.jalasoft.util.text;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by honzales on 18.4.15.
*/
public final class RegexFragment extends Fragment {
private final Pattern pattern;
private final String text;
private final List groups;
public RegexFragment(Matcher matcher) {
pattern = matcher.pattern();
text = matcher.group();
groups = initGroups(matcher);
}
private List initGroups(Matcher matcher) {
List groups = new ArrayList<>();
for(int i=1; i <= matcher.groupCount(); i++) {
String group = matcher.group(i);
groups.add(new TextFragment(group));
}
return groups;
}
@Override
public String text() {
return text;
}
public TextFragment asTextFragment() {
return new TextFragment(text);
}
public List getGroupFragments() {
return new ArrayList(groups);
}
public TextFragment getGroupTextFragment(int index) {
return groups.get(index);
}
public int getGroupCount() {
return groups.size();
}
@Override
RegexFragment getThis() {
return this;
}
}