@@ -44,50 +44,104 @@ public RegexException(final String message) {
4444 }
4545 }
4646
47+ @ Nonnull
48+ public static Matcher matchOrThrow (@ Nonnull final Pattern pattern ,
49+ final String input ) throws RegexException {
50+ final Matcher matcher = pattern .matcher (input );
51+ if (matcher .find ()) {
52+ return matcher ;
53+ } else {
54+ String errorMessage = "Failed to find pattern \" " + pattern .pattern () + "\" " ;
55+ if (input .length () <= 1024 ) {
56+ errorMessage += " inside of \" " + input + "\" " ;
57+ }
58+ throw new RegexException (errorMessage );
59+ }
60+ }
61+
62+ @ Nonnull
63+ public static String matchNamedGroup (final String pattern ,
64+ final String input ,
65+ final String groupName ) throws RegexException {
66+ return matchNamedGroup (Pattern .compile (pattern ), input , groupName );
67+ }
68+
69+ @ Nonnull
70+ public static String matchNamedGroup (@ Nonnull final Pattern pattern ,
71+ final String input ,
72+ @ Nonnull final String groupName ) throws RegexException {
73+ return matchOrThrow (pattern , input ).group (groupName );
74+ }
75+
76+ public static int getStartIndexOfNamedGroup (@ Nonnull final Pattern pattern ,
77+ final String input ,
78+ @ Nonnull final String groupName )
79+ throws RegexException {
80+ return matchOrThrow (pattern , input ).start (groupName );
81+ }
82+
83+ public static int getEndIndexOfNamedGroup (@ Nonnull final Pattern pattern ,
84+ final String input ,
85+ @ Nonnull final String groupName )
86+ throws RegexException {
87+ return matchOrThrow (pattern , input ).end (groupName );
88+ }
89+
90+ @ Nonnull
4791 public static String matchGroup1 (final String pattern , final String input )
4892 throws RegexException {
4993 return matchGroup (pattern , input , 1 );
5094 }
5195
96+
97+ @ Nonnull
5298 public static String matchGroup1 (final Pattern pattern ,
53- final String input ) throws RegexException {
99+ final String input ) throws RegexException {
54100 return matchGroup (pattern , input , 1 );
55101 }
56-
102+
103+ /**
104+ * Matches the specified group of the given pattern against the input.
105+ *
106+ * @param pattern The regex pattern to match.
107+ * @param input The input string to match against.
108+ * @param group The group number to retrieve (1-based index).
109+ * @return The matching group as a string.
110+ * @throws RegexException If the pattern does not match the input or if the group is not found.
111+ */
112+ @ Nonnull
57113 public static String matchGroup (final String pattern ,
58- final String input ,
59- final int group ) throws RegexException {
114+ final String input ,
115+ final int group ) throws RegexException {
60116 return matchGroup (Pattern .compile (pattern ), input , group );
61117 }
62-
63- public static String matchGroup (@ Nonnull final Pattern pat ,
118+
119+ @ Nonnull
120+ public static String matchGroup (@ Nonnull final Pattern pattern ,
64121 final String input ,
65122 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- }
123+ return matchOrThrow (pattern , input ).group (group );
79124 }
80125
81126 public static String matchGroup1MultiplePatterns (final Pattern [] patterns , final String input )
82127 throws RegexException {
83128 return matchMultiplePatterns (patterns , input ).group (1 );
84129 }
85130
131+ /**
132+ * Matches multiple patterns against the input string and
133+ * returns the first successful matcher
134+ *
135+ * @param patterns The array of regex patterns to match.
136+ * @param input The input string to match against.
137+ * @return A {@code Matcher} for the first successful match.
138+ * @throws RegexException If no patterns match the input or if {@code patterns} is empty.
139+ */
86140 public static Matcher matchMultiplePatterns (final Pattern [] patterns , final String input )
87141 throws RegexException {
88- Parser . RegexException exception = null ;
89- for (final Pattern pattern : patterns ) {
90- final Matcher matcher = pattern .matcher (input );
142+ RegexException exception = null ;
143+ for (final var pattern : patterns ) {
144+ final var matcher = pattern .matcher (input );
91145 if (matcher .find ()) {
92146 return matcher ;
93147 } else if (exception == null ) {
@@ -110,14 +164,11 @@ public static Matcher matchMultiplePatterns(final Pattern[] patterns, final Stri
110164 }
111165
112166 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 ();
167+ return isMatch (Pattern .compile (pattern ), input );
116168 }
117169
118170 public static boolean isMatch (@ Nonnull final Pattern pattern , final String input ) {
119- final Matcher mat = pattern .matcher (input );
120- return mat .find ();
171+ return pattern .matcher (input ).find ();
121172 }
122173
123174 @ Nonnull
0 commit comments