Skip to content

Added list of matching groups #28 #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/java/ru/lanwen/verbalregex/VerbalExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static java.lang.String.valueOf;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -746,6 +748,21 @@ public String getText(final String toTest, final int group) {
return result.toString();
}

/**
* Extract exact group from string and add it to list
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add example to javadoc of how it works?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but i can do this a little bit later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be in separate pr so, i'll merge and make release

* @param toTest - string to extract from
* @param group - group to extract
* @return list of extracted groups
*/
public List<String> getTextGroups(final String toTest, final int group) {
List<String> groups = new ArrayList<>();
Matcher m = pattern.matcher(toTest);
while (m.find()) {
groups.add(m.group(group));
}
return groups;
}

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.junit.Test;

import java.util.List;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static ru.lanwen.verbalregex.VerbalExpression.regex;
Expand Down Expand Up @@ -606,4 +608,23 @@ public void shouldAddMaybeWithOneOfFromAnotherBuilder() {
assertThat("Is a name without prefix", name, matchesTo("James"));

}

@Test
public void testListOfTextGroups() {
String text = "abczbcayyy";
VerbalExpression regex = regex()
.capture()
.oneOf("abc", "bca", "yyy")
.build();

List<String> groups = regex.getTextGroups(text, 1);

assertThat(groups.get(0), equalTo("abc"));
assertThat(groups.get(1), equalTo("bca"));
assertThat(groups.get(2), equalTo("yyy"));

String concatenatedText = groups.get(0) + groups.get(1) + groups.get(2);

assertThat(regex.getText(text, 1), equalTo(concatenatedText));
}
}