プログラムで使用するaaa.txt
neko\\inu\\saru\\kiji
実際のプログラム(junit4.8.2使用)
package kijitoraneko;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.junit.internal.matchers.IsCollectionContaining.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.junit.Test;
public class TestRegEx {
@Test
public void testStringMatches() {
String regex1 = "neko inu saru kiji";
String searchStr = "neko inu saru kiji";
boolean bool = searchStr.matches(regex1);
assertThat(bool, is(true));
regex1 = "neko";
searchStr = "neko inu saru kiji";
bool = searchStr.matches(regex1);
assertThat(bool, is(false));
}
@Test
public void testStringSplit() {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("aaa.txt"));
String regex = "\\\\\\\\";
String line = br.readLine();
String[] splited = line.split(regex);
List<String> splitList = Arrays.asList(splited);
String[] array = new String[] { "neko", "inu", "saru", "kiji" };
List<String> asList = Arrays.asList(array);
assertArrayEquals(array, splited);
assertThat(splitList, hasItems(asList.get(0), asList.get(1), asList.get(2), asList.get(3)));
} catch (IOException e) {
e.printStackTrace();
}
}
@Test(expected = PatternSyntaxException.class)
public void testStringSplitErr() {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("aaa.txt"));
String regex = "\\";
String line = br.readLine();
line.split(regex);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testMatcherMatches() {
String regex1 = "neko inu saru kiji";
String searchStr = "neko inu saru kiji";
Pattern pt = Pattern.compile(regex1);
Matcher matcher = pt.matcher(searchStr);
boolean bool = matcher.matches();
assertThat(bool, is(true));
regex1 = "neko";
searchStr = "neko inu saru kiji";
pt = Pattern.compile(regex1);
matcher = pt.matcher(searchStr);
boolean bool2 = matcher.matches();
assertThat(bool2, is(false));
}
@Test
public void testMatcherFind() {
String regex1 = "inu";
String searchStr = "neko inu saru kiji";
Pattern pt = Pattern.compile(regex1);
Matcher matcher = pt.matcher(searchStr);
boolean bool = matcher.find();
assertThat(bool, is(true));
bool = matcher.find();
assertThat(bool, is(false));
matcher.reset();
bool = matcher.find();
assertThat(bool, is(true));
}
@Test
public void testMatcherLookAt() {
String regex1 = "inu";
String searchStr = "neko inu saru kiji";
Pattern pt = Pattern.compile(regex1);
Matcher matcher = pt.matcher(searchStr);
boolean bool = matcher.lookingAt();
assertThat(bool, is(false));
regex1 = "neko";
searchStr = "neko inu saru kiji";
pt = Pattern.compile(regex1);
matcher = pt.matcher(searchStr);
bool = matcher.lookingAt();
assertThat(bool, is(true));
}
@Test
public void testBackRefer() {
String regex1 = "((neko).+)(\\2zura)";
String searchStr = "neko inu saru kiji neko inu nekozura";
Pattern pt = Pattern.compile(regex1);
Matcher matcher = pt.matcher(searchStr);
matcher.find();
String all = matcher.group(0);
String f = matcher.group(1);
String b = matcher.group(3);
assertThat(all, is("neko inu saru kiji neko inu nekozura"));
assertThat(f, is("neko inu saru kiji neko inu "));
assertThat(b, is("nekozura"));
}
}