@@ -44,50 +44,120 @@ public RegexException(final String message) {
4444 }
4545 }
4646
47+ /**
48+ * Matches input to the pattern or throw an exception if it doesn't match
49+ * @param pattern Pattern to match against
50+ * @param input Input to check if it matches pattern
51+ * @return The matcher after {@code find() == true}
52+ * @throws RegexException if {@code find() == false}
53+ */
54+ @ Nonnull
55+ public static Matcher matchOrThrow (@ Nonnull final Pattern pattern ,
56+ final String input ) throws RegexException {
57+ final Matcher matcher = pattern .matcher (input );
58+ if (matcher .find ()) {
59+ return matcher ;
60+ } else {
61+ String errorMessage = "Failed to find pattern \" " + pattern .pattern () + "\" " ;
62+ if (input .length () <= 1024 ) {
63+ errorMessage += " inside of \" " + input + "\" " ;
64+ }
65+ throw new RegexException (errorMessage );
66+ }
67+ }
68+
69+ /**
70+ * Matches group 1 of the given pattern against the input
71+ * and returns the matched group
72+ *
73+ * @param pattern The regex pattern to match.
74+ * @param input The input string to match against.
75+ * @return The matching group as a string.
76+ * @throws RegexException If the pattern does not match the input or if the group is not found.
77+ */
78+ @ Nonnull
4779 public static String matchGroup1 (final String pattern , final String input )
4880 throws RegexException {
4981 return matchGroup (pattern , input , 1 );
5082 }
5183
52- public static String matchGroup1 (final Pattern pattern ,
53- final String input ) throws RegexException {
84+ /**
85+ * Matches group 1 of the given pattern against the input
86+ * and returns the matched group
87+ *
88+ * @param pattern The regex pattern to match.
89+ * @param input The input string to match against.
90+ * @return The matching group as a string.
91+ * @throws RegexException If the pattern does not match the input or if the group is not found.
92+ */
93+ @ Nonnull
94+ public static String matchGroup1 (final Pattern pattern , final String input )
95+ throws RegexException {
5496 return matchGroup (pattern , input , 1 );
5597 }
5698
57- public static String matchGroup (final String pattern ,
58- final String input ,
59- final int group ) throws RegexException {
99+ /**
100+ * Matches the specified group of the given pattern against the input,
101+ * and returns the matched group
102+ *
103+ * @param pattern The regex pattern to match.
104+ * @param input The input string to match against.
105+ * @param group The group number to retrieve (1-based index).
106+ * @return The matching group as a string.
107+ * @throws RegexException If the pattern does not match the input or if the group is not found.
108+ */
109+ @ Nonnull
110+ public static String matchGroup (final String pattern , final String input , final int group )
111+ throws RegexException {
60112 return matchGroup (Pattern .compile (pattern ), input , group );
61113 }
62114
63- public static String matchGroup (@ Nonnull final Pattern pat ,
115+ /**
116+ * Matches the specified group of the given pattern against the input,
117+ * and returns the matched group
118+ *
119+ * @param pattern The regex pattern to match.
120+ * @param input The input string to match against.
121+ * @param group The group number to retrieve (1-based index).
122+ * @return The matching group as a string.
123+ * @throws RegexException If the pattern does not match the input or if the group is not found.
124+ */
125+ @ Nonnull
126+ public static String matchGroup (@ Nonnull final Pattern pattern ,
64127 final String input ,
65- final int group ) throws RegexException {
66- final Matcher matcher = pat .matcher (input );
67- final boolean foundMatch = matcher .find ();
68- if (foundMatch ) {
69- return matcher .group (group );
70- } else {
71- // only pass input to exception message when it is not too long
72- if (input .length () > 1024 ) {
73- throw new RegexException ("Failed to find pattern \" " + pat .pattern () + "\" " );
74- } else {
75- throw new RegexException ("Failed to find pattern \" " + pat .pattern ()
76- + "\" inside of \" " + input + "\" " );
77- }
78- }
128+ final int group )
129+ throws RegexException {
130+ return matchOrThrow (pattern , input ).group (group );
79131 }
80132
133+ /**
134+ * Matches multiple patterns against the input string and
135+ * returns the first successful matcher
136+ *
137+ * @param patterns The array of regex patterns to match.
138+ * @param input The input string to match against.
139+ * @return A {@code Matcher} for the first successful match.
140+ * @throws RegexException If no patterns match the input or if {@code patterns} is empty.
141+ */
81142 public static String matchGroup1MultiplePatterns (final Pattern [] patterns , final String input )
82143 throws RegexException {
83144 return matchMultiplePatterns (patterns , input ).group (1 );
84145 }
85146
147+ /**
148+ * Matches multiple patterns against the input string and
149+ * returns the first successful matcher
150+ *
151+ * @param patterns The array of regex patterns to match.
152+ * @param input The input string to match against.
153+ * @return A {@code Matcher} for the first successful match.
154+ * @throws RegexException If no patterns match the input or if {@code patterns} is empty.
155+ */
86156 public static Matcher matchMultiplePatterns (final Pattern [] patterns , final String input )
87157 throws RegexException {
88- Parser . RegexException exception = null ;
89- for (final Pattern pattern : patterns ) {
90- final Matcher matcher = pattern .matcher (input );
158+ RegexException exception = null ;
159+ for (final var pattern : patterns ) {
160+ final var matcher = pattern .matcher (input );
91161 if (matcher .find ()) {
92162 return matcher ;
93163 } else if (exception == null ) {
@@ -110,14 +180,11 @@ public static Matcher matchMultiplePatterns(final Pattern[] patterns, final Stri
110180 }
111181
112182 public static boolean isMatch (final String pattern , final String input ) {
113- final Pattern pat = Pattern .compile (pattern );
114- final Matcher mat = pat .matcher (input );
115- return mat .find ();
183+ return isMatch (Pattern .compile (pattern ), input );
116184 }
117185
118186 public static boolean isMatch (@ Nonnull final Pattern pattern , final String input ) {
119- final Matcher mat = pattern .matcher (input );
120- return mat .find ();
187+ return pattern .matcher (input ).find ();
121188 }
122189
123190 @ Nonnull
0 commit comments