[BAEL-5518] Create Array of Regex Matches (#12052)

* opokharel's code for "A quick and practical example of Hexagonal Architecture in Java"

* opokharel - added unit Tests

* [BAEL-5518]  by @opokharel

* updated Files

* updated formatting

* whitespaceFix

* [BAEL-5518] Create Array of Regex Matches

* reCreatingPR
This commit is contained in:
opokharel 2022-04-11 21:17:54 -06:00 committed by GitHub
parent add090f2e1
commit f16bd2b949
2 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,24 @@
package regex.array;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.*;
class RegexMatches {
String[] regexMatch(String strSearch)
{
List<String> matchesList = new ArrayList<String>();
String stringToSearch = strSearch;
Pattern p1 = Pattern.compile("780{1}\\d{7}");
Matcher m1 = p1.matcher(stringToSearch);
while (m1.find())
{
matchesList.add(m1.group());
}
int sizeOfNewArray = matchesList.size();
String newArrayOfMatches[] = new String[sizeOfNewArray];
matchesList.toArray(newArrayOfMatches);
return newArrayOfMatches;
}
}

View File

@ -0,0 +1,33 @@
package regex.array;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import regex.array.RegexMatches;
class RegexMatchesUnitTest {
@Test
void whenFourNums_thenFourMatches() {
RegexMatches rm = new RegexMatches();
String actual[] = rm.regexMatch("7801111211fsdafasdfa 7802222222 sadfsadfsda7803333333 sadfdasfasd 7804444444");
assertArrayEquals(new String[] {"7801111211", "7802222222", "7803333333", "7804444444"}, actual, "success");
}
@Test
void whenThreeNums_thenThreeMatches() {
RegexMatches rm = new RegexMatches();
String actual[] = rm.regexMatch("7801111211fsdafasdfa 780222222 sadfsadfsda7803333333 sadfdasfasd 7804444444");
assertArrayEquals(new String[] {"7801111211", "7803333333", "7804444444"}, actual, "success");
}
@Test
void whenZeroNums_thenZeroMatches() {
RegexMatches rm = new RegexMatches();
String actual[] = rm.regexMatch("78011111fsdafasdfa 780222222 sadfsadfsda78033333 sadfdasfasd 7804444");
assertArrayEquals(new String[] {}, actual, "success");
}
}