Class RegexUtil
java.lang.Object
org.checkerframework.checker.regex.util.RegexUtil
Utility methods for regular expressions, most notably for testing whether a string is a regular
expression.
For an example of intended use, see section Testing whether a string is a regular expression in the Checker Framework manual.
Runtime Dependency: If you use this class, you must distribute (or link to)
checker-qual.jar, along with your binaries. Or, you can copy this class into your own project.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionReturns the argument as a@Regex Stringif it is a regex, otherwise throws an error.Returns the argument as a@Regex(groups) Stringif it is a regex with at least the given number of groups, otherwise throws an error.static booleanisRegex(char c) Returns true if the argument is a syntactically valid regular expression.static booleanReturns true if the argument is a syntactically valid regular expression.static booleanReturns true if the argument is a syntactically valid regular expression with at least the given number of groups.regexError(String s) Returns null if the argument is a syntactically valid regular expression.regexError(String s, int groups) Returns null if the argument is a syntactically valid regular expression with at least the given number of groups.static @Nullable PatternSyntaxExceptionReturns null if the argument is a syntactically valid regular expression.static @Nullable PatternSyntaxExceptionregexException(String s, int groups) Returns null if the argument is a syntactically valid regular expression with at least the given number of groups.
-
Method Details
-
isRegex
@Pure @EnsuresQualifierIf(result=true, expression="#1", qualifier=Regex.class) public static boolean isRegex(String s) Returns true if the argument is a syntactically valid regular expression.- Parameters:
s- string to check for being a regular expression- Returns:
- true iff s is a regular expression
-
isRegex
@Pure @EnsuresQualifierIf(result=true, expression="#1", qualifier=Regex.class) public static boolean isRegex(String s, int groups) Returns true if the argument is a syntactically valid regular expression with at least the given number of groups.- Parameters:
s- string to check for being a regular expressiongroups- number of groups expected- Returns:
- true iff s is a regular expression with
groupsgroups
-
isRegex
@Pure @EnsuresQualifierIf(result=true, expression="#1", qualifier=Regex.class) public static boolean isRegex(char c) Returns true if the argument is a syntactically valid regular expression.- Parameters:
c- char to check for being a regular expression- Returns:
- true iff c is a regular expression
-
regexError
Returns null if the argument is a syntactically valid regular expression. Otherwise returns a string describing why the argument is not a regex.- Parameters:
s- string to check for being a regular expression- Returns:
- null, or a string describing why the argument is not a regex
-
regexError
Returns null if the argument is a syntactically valid regular expression with at least the given number of groups. Otherwise returns a string describing why the argument is not a regex.- Parameters:
s- string to check for being a regular expressiongroups- number of groups expected- Returns:
- null, or a string describing why the argument is not a regex
-
regexException
Returns null if the argument is a syntactically valid regular expression. Otherwise returns a PatternSyntaxException describing why the argument is not a regex.- Parameters:
s- string to check for being a regular expression- Returns:
- null, or a PatternSyntaxException describing why the argument is not a regex
-
regexException
Returns null if the argument is a syntactically valid regular expression with at least the given number of groups. Otherwise returns a PatternSyntaxException describing why the argument is not a regex.- Parameters:
s- string to check for being a regular expressiongroups- number of groups expected- Returns:
- null, or a PatternSyntaxException describing why the argument is not a regex
-
asRegex
Returns the argument as a@Regex Stringif it is a regex, otherwise throws an error.The purpose of this method is to suppress Regex Checker warnings. It should be very rarely needed.
- Parameters:
s- string to check for being a regular expression- Returns:
- its argument
- Throws:
Error- if argument is not a regex
-
asRegex
Returns the argument as a@Regex(groups) Stringif it is a regex with at least the given number of groups, otherwise throws an error.The purpose of this method is to suppress Regex Checker warnings. It should be very rarely needed.
- Parameters:
s- string to check for being a regular expressiongroups- number of groups expected- Returns:
- its argument
- Throws:
Error- if argument is not a regex
-