-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegexComparisonTest.java
61 lines (43 loc) · 2.13 KB
/
RegexComparisonTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import org.junit.jupiter.api.Test;
import utils.TestUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.assertFalse;
public class RegexComparisonTest {
@Test
public void standardRegexWithEmailInTextWithoutBacktrackingIssueTest() {
String email = "[email protected]";
long start = System.currentTimeMillis();
String inputText = TestUtils.getRandomLongWord(email);
Matcher matcher = Pattern.compile(TestUtils.emailRegex).matcher(inputText);
String actualValue = matcher.replaceAll("[email protected]");
System.out.println(System.currentTimeMillis() - start); // it's about 38 ms
assertFalse(actualValue.contains(email));
}
@Test
public void standardRegexWithoutEmailInTextWithBacktrackingIssueTest() {
long start = System.currentTimeMillis();
String inputText = TestUtils.getRandomLongWord("");
Matcher matcher = Pattern.compile(TestUtils.emailRegex).matcher(inputText);
matcher.replaceAll("[email protected]");
System.out.println(System.currentTimeMillis() - start); // it's about 8308 ms
}
@Test
public void re2jRegexWithEmailInTextWithoutBacktrackingIssueTest() {
String email = "[email protected]";
long start = System.currentTimeMillis();
String inputText = TestUtils.getRandomLongWord("[email protected]");
com.google.re2j.Matcher matcher = com.google.re2j.Pattern.compile(TestUtils.emailRegex).matcher(inputText);
String actualValue = matcher.replaceAll("[email protected]");
System.out.println(System.currentTimeMillis() - start); // it's about 55 ms
assertFalse(actualValue.contains(email));
}
@Test
public void re2jRegexWithoutEmailInTextWithoutBacktrackingIssueTest() {
long start = System.currentTimeMillis();
String inputText = TestUtils.getRandomLongWord("");
com.google.re2j.Matcher matcher = com.google.re2j.Pattern.compile(TestUtils.emailRegex).matcher(inputText);
matcher.replaceAll("[email protected]");
System.out.println(System.currentTimeMillis() - start); // it's about 72 ms
}
}