Skip to content

Commit c397574

Browse files
author
Mark Whitaker
authored
Add new elements (#16)
* Added new whitespace RegexBuilder methods * Added RegexBuilder.possibleWhitespace() * Deprecated RegexQuantifier.noneOrOne(), replaced with zeroOrOne() * Changed RegexBuilder.anyOf() to take variable args and fixed bug in anyOf(String, RegexQuantifier) * Bug fix in makeSafeForRegex()
1 parent 4ad4d66 commit c397574

3 files changed

Lines changed: 380 additions & 11 deletions

File tree

src/main/java/uk/co/mainwave/regextoolbox/RegexBuilder.java

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,102 @@ public RegexBuilder nonWhitespace(final RegexQuantifier quantifier) {
218218
return this;
219219
}
220220

221+
/**
222+
* Add an element to represent any amount of white space, including none. This is just a convenient alias for
223+
* {@code whitespace(RegexQuantifier.zeroOrMore())}.
224+
*
225+
* @return The current {@link RegexBuilder} object, for method chaining
226+
*/
227+
public RegexBuilder possibleWhitespace() {
228+
return whitespace(RegexQuantifier.zeroOrMore());
229+
}
230+
231+
/**
232+
* Add an element to match a single space character. If you want to match any kind of white space, use
233+
* {@link #whitespace()}.
234+
*
235+
* @return The current {@link RegexBuilder} object, for method chaining
236+
*/
237+
public RegexBuilder space() {
238+
return space(null);
239+
}
240+
241+
/**
242+
* Add an element to match a single space character. If you want to match any kind of white space, use
243+
* {@link #whitespace()}.
244+
*
245+
* @param quantifier Quantifier to apply to this element
246+
* @return The current {@link RegexBuilder} object, for method chaining
247+
*/
248+
public RegexBuilder space(final RegexQuantifier quantifier) {
249+
stringBuilder.append(" ");
250+
addQuantifier(quantifier);
251+
return this;
252+
}
253+
254+
/**
255+
* Add an element to match a single tab character.
256+
*
257+
* @return The current {@link RegexBuilder} object, for method chaining
258+
*/
259+
public RegexBuilder tab() {
260+
return tab(null);
261+
}
262+
263+
/**
264+
* Add an element to match a single tab character.
265+
*
266+
* @param quantifier Quantifier to apply to this element
267+
* @return The current {@link RegexBuilder} object, for method chaining
268+
*/
269+
public RegexBuilder tab(final RegexQuantifier quantifier) {
270+
stringBuilder.append("\\t");
271+
addQuantifier(quantifier);
272+
return this;
273+
}
274+
275+
/**
276+
* Add an element to match a single line feed character.
277+
*
278+
* @return The current {@link RegexBuilder} object, for method chaining
279+
*/
280+
public RegexBuilder lineFeed() {
281+
return lineFeed(null);
282+
}
283+
284+
/**
285+
* Add an element to match a single line feed character.
286+
*
287+
* @param quantifier Quantifier to apply to this element
288+
* @return The current {@link RegexBuilder} object, for method chaining
289+
*/
290+
public RegexBuilder lineFeed(final RegexQuantifier quantifier) {
291+
stringBuilder.append("\\n");
292+
addQuantifier(quantifier);
293+
return this;
294+
}
295+
296+
/**
297+
* Add an element to match a single carriage return character.
298+
*
299+
* @return The current {@link RegexBuilder} object, for method chaining
300+
*/
301+
public RegexBuilder carriageReturn() {
302+
return carriageReturn(null);
303+
}
304+
305+
/**
306+
* Add an element to match a single carriage return character.
307+
*
308+
* @param quantifier Quantifier to apply to this element
309+
* @return The current {@link RegexBuilder} object, for method chaining
310+
*/
311+
public RegexBuilder carriageReturn(final RegexQuantifier quantifier) {
312+
stringBuilder.append("\\r");
313+
addQuantifier(quantifier);
314+
return this;
315+
}
316+
221317
/**
222318
* Add an element to match any single decimal digit (0-9).
223319
*
@@ -574,7 +670,7 @@ public RegexBuilder anyCharacterExcept(final String characters, final RegexQuant
574670
* @param strings A number of strings, any one of which will be matched
575671
* @return The current {@link RegexBuilder} object, for method chaining
576672
*/
577-
public RegexBuilder anyOf(final String[] strings) {
673+
public RegexBuilder anyOf(final String... strings) {
578674
return anyOf(strings, null);
579675
}
580676

@@ -604,7 +700,7 @@ public RegexBuilder anyOf(final String[] strings, final RegexQuantifier quantifi
604700
RegexBuilder builder = null;
605701
try {
606702
builder = startNonCapturingGroup()
607-
.regexText(String.join("|", safeStrings), quantifier)
703+
.regexText(String.join("|", safeStrings))
608704
.endGroup(quantifier);
609705
} catch (RegexBuilderException ignored) {
610706
// We won't get an exception from endGroup() as we know we started the group properly
@@ -760,8 +856,7 @@ private String makeSafeForCharacterClass(final String s) {
760856
}
761857

762858
private static String makeSafeForRegex(final String s) {
763-
764-
return s
859+
return (s == null) ? "" : s
765860
// Make sure this always comes first!
766861
.replace("\\", "\\\\")
767862
.replace("?", "\\?")

src/main/java/uk/co/mainwave/regextoolbox/RegexQuantifier.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,24 @@ public static RegexGreedyQuantifier oneOrMore() {
3030

3131
/**
3232
* Quantifier to match the preceding element once or not at all
33+
* @deprecated Use {@link #zeroOrOne()} instead
3334
*
3435
* @return A greedy quantifier: use {@link RegexGreedyQuantifier#butAsFewAsPossible()} to make it non-greedy
3536
*/
37+
@Deprecated
3638
public static RegexGreedyQuantifier noneOrOne() {
3739
return new RegexGreedyQuantifier("?");
3840
}
3941

42+
/**
43+
* Quantifier to match the preceding element once or not at all
44+
*
45+
* @return A greedy quantifier: use {@link RegexGreedyQuantifier#butAsFewAsPossible()} to make it non-greedy
46+
*/
47+
public static RegexGreedyQuantifier zeroOrOne() {
48+
return new RegexGreedyQuantifier("?");
49+
}
50+
4051
/**
4152
* Quantifier to match an exact number of occurrences of the preceding element
4253
*

0 commit comments

Comments
 (0)