Skip to content

Commit ea7679a

Browse files
author
MerkushevKirill
committed
add - predefined character classes
1 parent d30d7f4 commit ea7679a

File tree

2 files changed

+159
-22
lines changed

2 files changed

+159
-22
lines changed

src/main/java/ru/lanwen/verbalregex/VerbalExpression.java

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,67 +52,116 @@ public Builder endOfLine() {
5252
}
5353

5454
public Builder then(String pValue) {
55-
this.add("(?:" + sanitize(pValue) + ")");
56-
return this;
55+
return this.add("(?:" + sanitize(pValue) + ")");
5756
}
5857

5958
public Builder find(String value) {
60-
this.then(value);
61-
return this;
59+
return this.then(value);
6260
}
6361

6462
public Builder maybe(final String pValue) {
6563
return this.then(pValue).add("?");
6664
}
6765

6866
public Builder anything() {
69-
this.add("(?:.*)");
70-
return this;
67+
return this.add("(?:.*)");
7168
}
7269

7370
public Builder anythingButNot(final String pValue) {
74-
this.add("(?:[^" + sanitize(pValue) + "]*)");
75-
return this;
71+
return this.add("(?:[^" + sanitize(pValue) + "]*)");
7672
}
7773

7874
public Builder something() {
79-
this.add("(?:.+)");
80-
return this;
75+
return this.add("(?:.+)");
8176
}
8277

8378
public Builder somethingButNot(final String pValue) {
84-
this.add("(?:[^" + sanitize(pValue) + "]+)");
85-
return this;
79+
return this.add("(?:[^" + sanitize(pValue) + "]+)");
8680
}
8781

8882
public Builder lineBreak() {
89-
this.add("(?:\\n|(\\r\\n))");
90-
return this;
83+
return this.add("(?:\\n|(\\r\\n))");
9184
}
9285

9386
public Builder br() {
94-
this.lineBreak();
95-
return this;
87+
return this.lineBreak();
9688
}
9789

90+
/**
91+
* @return tab character ('\u0009')
92+
*/
9893
public Builder tab() {
99-
this.add("\\t");
100-
return this;
94+
return this.add("(?:\\t)");
10195
}
10296

97+
/**
98+
* @return word, same as [a-zA-Z_0-9]+
99+
*/
103100
public Builder word() {
104-
this.add("\\w+");
105-
return this;
101+
return this.add("(?:\\w+)");
102+
}
103+
104+
105+
/*
106+
--- Predefined character classes
107+
*/
108+
109+
/**
110+
* @return word character, same as [a-zA-Z_0-9]
111+
*/
112+
public Builder wordChar() {
113+
return this.add("(?:\\w)");
106114
}
107115

116+
117+
/**
118+
* @return non-word character: [^\w]
119+
*/
120+
public Builder nonWordChar() {
121+
return this.add("(?:\\W)");
122+
}
123+
124+
/**
125+
* @return non-digit: [^0-9]
126+
*/
127+
public Builder nonDigit() {
128+
return this.add("(?:\\D)");
129+
}
130+
131+
/**
132+
* @return same as [0-9]
133+
*/
134+
public Builder digit() {
135+
return this.add("(?:\\d)");
136+
}
137+
138+
/**
139+
* @return whitespace character, same as [ \t\n\x0B\f\r]
140+
*/
141+
public Builder space() {
142+
return this.add("(?:\\s)");
143+
}
144+
145+
/**
146+
* @return non-whitespace character: [^\s]
147+
*/
148+
public Builder nonSpace() {
149+
return this.add("(?:\\S)");
150+
}
151+
152+
153+
/*
154+
--- / end of predefined character classes
155+
*/
156+
157+
108158
public Builder anyOf(final String pValue) {
109159
this.add("[" + sanitize(pValue) + "]");
110160
return this;
111161
}
112162

113163
public Builder any(final String value) {
114-
this.anyOf(value);
115-
return this;
164+
return this.anyOf(value);
116165
}
117166

118167
public Builder range(String... pArgs) {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package ru.lanwen.verbalregex;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.equalTo;
6+
import static org.hamcrest.CoreMatchers.is;
7+
import static org.hamcrest.CoreMatchers.not;
8+
import static org.hamcrest.MatcherAssert.assertThat;
9+
import static ru.lanwen.verbalregex.VerbalExpression.regex;
10+
11+
/**
12+
* User: lanwen
13+
* Date: 13.05.14
14+
* Time: 16:26
15+
*/
16+
public class PredefinedCharClassesTest {
17+
18+
public static final String LETTERS_NO_DIGITS = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_";
19+
public static final String DIGITS = "0123456789";
20+
public static final String NON_LETTERS = ";'[]{}|?/";
21+
public static final String SPACE = " \t\n\f\r";
22+
23+
@Test
24+
public void testWordChar() throws Exception {
25+
VerbalExpression regex = regex().wordChar().build();
26+
27+
assertThat("Not matches on letters", regex.test(LETTERS_NO_DIGITS + DIGITS), is(true));
28+
assertThat("matches on non letters", regex.test(NON_LETTERS + SPACE), is(false));
29+
assertThat("Extracts wrong word chars",
30+
regex.getText(LETTERS_NO_DIGITS + DIGITS + NON_LETTERS + SPACE), equalTo(LETTERS_NO_DIGITS + DIGITS));
31+
32+
}
33+
34+
@Test
35+
public void testNonWordChar() throws Exception {
36+
VerbalExpression regex = regex().nonWordChar().build();
37+
38+
assertThat("matches on letters", regex.test(LETTERS_NO_DIGITS + DIGITS), is(false));
39+
assertThat("Not matches on non letters", regex.test(NON_LETTERS + SPACE), is(true));
40+
assertThat("Extracts wrong chars",
41+
regex.getText(LETTERS_NO_DIGITS + DIGITS + NON_LETTERS + SPACE), equalTo(NON_LETTERS + SPACE));
42+
43+
}
44+
45+
@Test
46+
public void testSpace() throws Exception {
47+
VerbalExpression regex = regex().space().build();
48+
49+
assertThat("matches on letters", regex.test(LETTERS_NO_DIGITS + DIGITS + NON_LETTERS), is(false));
50+
assertThat("Not matches on space", regex.test(SPACE), is(true));
51+
assertThat("Extracts wrong chars",
52+
regex.getText(LETTERS_NO_DIGITS + DIGITS + NON_LETTERS + SPACE), equalTo(SPACE));
53+
54+
}
55+
56+
@Test
57+
public void testNonSpace() throws Exception {
58+
VerbalExpression regex = regex().nonSpace().build();
59+
60+
assertThat("Not matches on non space", regex.test(LETTERS_NO_DIGITS + DIGITS + NON_LETTERS), is(true));
61+
assertThat("matches on space", regex.test(SPACE), is(false));
62+
assertThat("Extracts wrong chars",
63+
regex.getText(LETTERS_NO_DIGITS + DIGITS + NON_LETTERS + SPACE), not(SPACE));
64+
65+
}
66+
67+
@Test
68+
public void testDigit() throws Exception {
69+
VerbalExpression regex = regex().digit().build();
70+
71+
assertThat("matches on letters", regex.test(LETTERS_NO_DIGITS + SPACE + NON_LETTERS), is(false));
72+
assertThat("Not matches on digits", regex.test(DIGITS), is(true));
73+
assertThat("Extracts wrong chars",
74+
regex.getText(LETTERS_NO_DIGITS + DIGITS + NON_LETTERS + SPACE), is(DIGITS));
75+
76+
}
77+
78+
@Test
79+
public void testNonDigit() throws Exception {
80+
VerbalExpression regex = regex().nonDigit().build();
81+
82+
assertThat("Not matches on letters", regex.test(LETTERS_NO_DIGITS + SPACE + NON_LETTERS), is(true));
83+
assertThat("matches on digits", regex.test(DIGITS), is(false));
84+
assertThat("Extracts wrong chars",
85+
regex.getText(LETTERS_NO_DIGITS + DIGITS + NON_LETTERS + SPACE), not(DIGITS));
86+
87+
}
88+
}

0 commit comments

Comments
 (0)