Regular Expression Java – Mock Test Answers

The following string (inputs) is used to simulate a clients answers to five questions. The format is the question number followed by a delimiter and the alphabetic choice for that question.

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
public class CoreJavaApplicationTests {
 final String inputs = "1:A, 2:B, 3:B, 4:A, 5:C";
 final String pattern = "[0-9]:[a-zA-Z],*";

 public void parseAllSolution() {
 Matcher matchAll = Pattern.compile(pattern).matcher(inputs);

 private void printSections(Matcher matcher) {
 while (matcher.find()) {
 String item =;

 Integer id = Integer.valueOf(item.replaceAll(patternSuffix,"").replaceAll(",",""));
 System.out.printf("\n PREFIX %s%n", id);

 String meta = item.replaceAll(patternPrefix,"").replaceAll(",","");
 System.out.printf("SUFFIX %s%n", meta);



 private void disp(Integer question, String response) {
 System.out.printf("ID: %d, RESP: %s%n", question, response);

 baselist.forEach(item -> {
 if (item.getId() == question && item.getResponse().equalsIgnoreCase(response)) {
 System.out.printf("Choice: Question - %d, Answer - %s, Data - %s%n", question, item.getResponse(), item.getDescriptor());



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s