View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNull;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.util.regex.Pattern;
25  import java.util.regex.PatternSyntaxException;
26  
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Tests {@link org.apache.commons.lang3.RegExUtils}.
31   */
32  public class RegExUtilsTest extends AbstractLangTest {
33  
34      @Test
35      public void testDotAll() {
36          assertTrue(RegExUtils.dotAll("<A>.*</A>").matcher("<A>\nxy\n</A>").matches());
37      }
38  
39      @Test
40      public void testDotAllMatcher() {
41          assertTrue(RegExUtils.dotAllMatcher("<A>.*</A>", "<A>\nxy\n</A>").matches());
42      }
43  
44      @Test
45      public void testRemoveAll_StringPattern() {
46          assertNull(RegExUtils.removeAll(null, Pattern.compile("")));
47          assertEquals("any", RegExUtils.removeAll("any", (Pattern) null));
48  
49          assertEquals("any", RegExUtils.removeAll("any", Pattern.compile("")));
50          assertEquals("", RegExUtils.removeAll("any", Pattern.compile(".*")));
51          assertEquals("", RegExUtils.removeAll("any", Pattern.compile(".+")));
52          assertEquals("", RegExUtils.removeAll("any", Pattern.compile(".?")));
53  
54          assertEquals("A\nB", RegExUtils.removeAll("A<__>\n<__>B", Pattern.compile("<.*>")));
55          assertEquals("AB", RegExUtils.removeAll("A<__>\n<__>B", Pattern.compile("(?s)<.*>")));
56          assertEquals("ABC123", RegExUtils.removeAll("ABCabc123abc", Pattern.compile("[a-z]")));
57  
58          assertEquals("AB", RegExUtils.removeAll("A<__>\n<__>B", Pattern.compile("<.*>", Pattern.DOTALL)));
59          assertEquals("AB", RegExUtils.removeAll("A<__>\\n<__>B", Pattern.compile("<.*>")));
60          assertEquals("", RegExUtils.removeAll("<A>x\\ny</A>", Pattern.compile("<A>.*</A>")));
61          assertEquals("", RegExUtils.removeAll("<A>\nxy\n</A>", Pattern.compile("<A>.*</A>", Pattern.DOTALL)));
62      }
63  
64      @Test
65      public void testRemoveAll_StringString() {
66          assertNull(RegExUtils.removeAll(null, ""));
67          assertEquals("any", RegExUtils.removeAll("any", (String) null));
68  
69          assertEquals("any", RegExUtils.removeAll("any", ""));
70          assertEquals("", RegExUtils.removeAll("any", ".*"));
71          assertEquals("", RegExUtils.removeAll("any", ".+"));
72          assertEquals("", RegExUtils.removeAll("any", ".?"));
73  
74          assertEquals("A\nB", RegExUtils.removeAll("A<__>\n<__>B", "<.*>"));
75          assertEquals("AB", RegExUtils.removeAll("A<__>\n<__>B", "(?s)<.*>"));
76          assertEquals("ABC123", RegExUtils.removeAll("ABCabc123abc", "[a-z]"));
77  
78          assertThrows(
79                  PatternSyntaxException.class,
80                  () -> RegExUtils.removeAll("any", "{badRegexSyntax}"),
81                  "RegExUtils.removeAll expecting PatternSyntaxException");
82      }
83  
84      @Test
85      public void testRemoveFirst_StringPattern() {
86          assertNull(RegExUtils.removeFirst(null, Pattern.compile("")));
87          assertEquals("any", RegExUtils.removeFirst("any", (Pattern) null));
88  
89          assertEquals("any", RegExUtils.removeFirst("any", Pattern.compile("")));
90          assertEquals("", RegExUtils.removeFirst("any", Pattern.compile(".*")));
91          assertEquals("", RegExUtils.removeFirst("any", Pattern.compile(".+")));
92          assertEquals("bc", RegExUtils.removeFirst("abc", Pattern.compile(".?")));
93  
94          assertEquals("A\n<__>B", RegExUtils.removeFirst("A<__>\n<__>B", Pattern.compile("<.*>")));
95          assertEquals("AB", RegExUtils.removeFirst("A<__>\n<__>B", Pattern.compile("(?s)<.*>")));
96          assertEquals("ABCbc123", RegExUtils.removeFirst("ABCabc123", Pattern.compile("[a-z]")));
97          assertEquals("ABC123abc", RegExUtils.removeFirst("ABCabc123abc", Pattern.compile("[a-z]+")));
98      }
99  
100     @Test
101     public void testRemoveFirst_StringString() {
102         assertNull(RegExUtils.removeFirst(null, ""));
103         assertEquals("any", RegExUtils.removeFirst("any", (String) null));
104 
105         assertEquals("any", RegExUtils.removeFirst("any", ""));
106         assertEquals("", RegExUtils.removeFirst("any", ".*"));
107         assertEquals("", RegExUtils.removeFirst("any", ".+"));
108         assertEquals("bc", RegExUtils.removeFirst("abc", ".?"));
109 
110         assertEquals("A\n<__>B", RegExUtils.removeFirst("A<__>\n<__>B", "<.*>"));
111         assertEquals("AB", RegExUtils.removeFirst("A<__>\n<__>B", "(?s)<.*>"));
112         assertEquals("ABCbc123", RegExUtils.removeFirst("ABCabc123", "[a-z]"));
113         assertEquals("ABC123abc", RegExUtils.removeFirst("ABCabc123abc", "[a-z]+"));
114 
115         assertThrows(
116                 PatternSyntaxException.class,
117                 () -> RegExUtils.removeFirst("any", "{badRegexSyntax}"),
118                 "RegExUtils.removeFirst expecting PatternSyntaxException");
119     }
120 
121     @Test
122     public void testRemovePattern_StringString() {
123         assertNull(RegExUtils.removePattern(null, ""));
124         assertEquals("any", RegExUtils.removePattern("any", (String) null));
125 
126         assertEquals("", RegExUtils.removePattern("", ""));
127         assertEquals("", RegExUtils.removePattern("", ".*"));
128         assertEquals("", RegExUtils.removePattern("", ".+"));
129 
130         assertEquals("AB", RegExUtils.removePattern("A<__>\n<__>B", "<.*>"));
131         assertEquals("AB", RegExUtils.removePattern("A<__>\\n<__>B", "<.*>"));
132         assertEquals("", RegExUtils.removePattern("<A>x\\ny</A>", "<A>.*</A>"));
133         assertEquals("", RegExUtils.removePattern("<A>\nxy\n</A>", "<A>.*</A>"));
134 
135         assertEquals("ABC123", RegExUtils.removePattern("ABCabc123", "[a-z]"));
136     }
137 
138     @Test
139     public void testReplaceAll_StringPatternString() {
140         assertNull(RegExUtils.replaceAll(null, Pattern.compile(""), ""));
141 
142         assertEquals("any", RegExUtils.replaceAll("any", (Pattern) null, ""));
143         assertEquals("any", RegExUtils.replaceAll("any", Pattern.compile(""), null));
144 
145         assertEquals("zzz", RegExUtils.replaceAll("", Pattern.compile(""), "zzz"));
146         assertEquals("zzz", RegExUtils.replaceAll("", Pattern.compile(".*"), "zzz"));
147         assertEquals("", RegExUtils.replaceAll("", Pattern.compile(".+"), "zzz"));
148         assertEquals("ZZaZZbZZcZZ", RegExUtils.replaceAll("abc", Pattern.compile(""), "ZZ"));
149 
150         assertEquals("z\nz", RegExUtils.replaceAll("<__>\n<__>", Pattern.compile("<.*>"), "z"));
151         assertEquals("z", RegExUtils.replaceAll("<__>\n<__>", Pattern.compile("(?s)<.*>"), "z"));
152 
153         assertEquals("z", RegExUtils.replaceAll("<__>\n<__>", Pattern.compile("<.*>", Pattern.DOTALL), "z"));
154         assertEquals("z", RegExUtils.replaceAll("<__>\\n<__>", Pattern.compile("<.*>"), "z"));
155         assertEquals("X", RegExUtils.replaceAll("<A>\nxy\n</A>", Pattern.compile("<A>.*</A>", Pattern.DOTALL), "X"));
156 
157         assertEquals("ABC___123", RegExUtils.replaceAll("ABCabc123", Pattern.compile("[a-z]"), "_"));
158         assertEquals("ABC_123", RegExUtils.replaceAll("ABCabc123", Pattern.compile("[^A-Z0-9]+"), "_"));
159         assertEquals("ABC123", RegExUtils.replaceAll("ABCabc123", Pattern.compile("[^A-Z0-9]+"), ""));
160         assertEquals("Lorem_ipsum_dolor_sit",
161                 RegExUtils.replaceAll("Lorem ipsum  dolor   sit", Pattern.compile("( +)([a-z]+)"), "_$2"));
162     }
163 
164     @Test
165     public void testReplaceAll_StringStringString() {
166         assertNull(RegExUtils.replaceAll(null, "", ""));
167 
168         assertEquals("any", RegExUtils.replaceAll("any", (String) null, ""));
169         assertEquals("any", RegExUtils.replaceAll("any", "", null));
170 
171         assertEquals("zzz", RegExUtils.replaceAll("", "", "zzz"));
172         assertEquals("zzz", RegExUtils.replaceAll("", ".*", "zzz"));
173         assertEquals("", RegExUtils.replaceAll("", ".+", "zzz"));
174         assertEquals("ZZaZZbZZcZZ", RegExUtils.replaceAll("abc", "", "ZZ"));
175 
176         assertEquals("z\nz", RegExUtils.replaceAll("<__>\n<__>", "<.*>", "z"));
177         assertEquals("z", RegExUtils.replaceAll("<__>\n<__>", "(?s)<.*>", "z"));
178 
179         assertEquals("ABC___123", RegExUtils.replaceAll("ABCabc123", "[a-z]", "_"));
180         assertEquals("ABC_123", RegExUtils.replaceAll("ABCabc123", "[^A-Z0-9]+", "_"));
181         assertEquals("ABC123", RegExUtils.replaceAll("ABCabc123", "[^A-Z0-9]+", ""));
182         assertEquals("Lorem_ipsum_dolor_sit", RegExUtils.replaceAll("Lorem ipsum  dolor   sit", "( +)([a-z]+)", "_$2"));
183 
184         assertThrows(
185                 PatternSyntaxException.class,
186                 () -> RegExUtils.replaceAll("any", "{badRegexSyntax}", ""),
187                 "RegExUtils.replaceAll expecting PatternSyntaxException");
188     }
189 
190     @Test
191     public void testReplaceFirst_StringPatternString() {
192         assertNull(RegExUtils.replaceFirst(null, Pattern.compile(""), ""));
193 
194         assertEquals("any", RegExUtils.replaceFirst("any", (Pattern) null, ""));
195         assertEquals("any", RegExUtils.replaceFirst("any", Pattern.compile(""), null));
196 
197         assertEquals("zzz", RegExUtils.replaceFirst("", Pattern.compile(""), "zzz"));
198         assertEquals("zzz", RegExUtils.replaceFirst("", Pattern.compile(".*"), "zzz"));
199         assertEquals("", RegExUtils.replaceFirst("", Pattern.compile(".+"), "zzz"));
200         assertEquals("ZZabc", RegExUtils.replaceFirst("abc", Pattern.compile(""), "ZZ"));
201 
202         assertEquals("z\n<__>", RegExUtils.replaceFirst("<__>\n<__>", Pattern.compile("<.*>"), "z"));
203         assertEquals("z", RegExUtils.replaceFirst("<__>\n<__>", Pattern.compile("(?s)<.*>"), "z"));
204 
205         assertEquals("ABC_bc123", RegExUtils.replaceFirst("ABCabc123", Pattern.compile("[a-z]"), "_"));
206         assertEquals("ABC_123abc", RegExUtils.replaceFirst("ABCabc123abc", Pattern.compile("[^A-Z0-9]+"), "_"));
207         assertEquals("ABC123abc", RegExUtils.replaceFirst("ABCabc123abc", Pattern.compile("[^A-Z0-9]+"), ""));
208         assertEquals("Lorem_ipsum  dolor   sit",
209                 RegExUtils.replaceFirst("Lorem ipsum  dolor   sit", Pattern.compile("( +)([a-z]+)"), "_$2"));
210     }
211 
212     @Test
213     public void testReplaceFirst_StringStringString() {
214         assertNull(RegExUtils.replaceFirst(null, "", ""));
215 
216         assertEquals("any", RegExUtils.replaceFirst("any", (String) null, ""));
217         assertEquals("any", RegExUtils.replaceFirst("any", "", null));
218 
219         assertEquals("zzz", RegExUtils.replaceFirst("", "", "zzz"));
220         assertEquals("zzz", RegExUtils.replaceFirst("", ".*", "zzz"));
221         assertEquals("", RegExUtils.replaceFirst("", ".+", "zzz"));
222         assertEquals("ZZabc", RegExUtils.replaceFirst("abc", "", "ZZ"));
223 
224         assertEquals("z\n<__>", RegExUtils.replaceFirst("<__>\n<__>", "<.*>", "z"));
225         assertEquals("z", RegExUtils.replaceFirst("<__>\n<__>", "(?s)<.*>", "z"));
226 
227         assertEquals("ABC_bc123", RegExUtils.replaceFirst("ABCabc123", "[a-z]", "_"));
228         assertEquals("ABC_123abc", RegExUtils.replaceFirst("ABCabc123abc", "[^A-Z0-9]+", "_"));
229         assertEquals("ABC123abc", RegExUtils.replaceFirst("ABCabc123abc", "[^A-Z0-9]+", ""));
230         assertEquals("Lorem_ipsum  dolor   sit",
231                 RegExUtils.replaceFirst("Lorem ipsum  dolor   sit", "( +)([a-z]+)", "_$2"));
232 
233         assertThrows(
234                 PatternSyntaxException.class,
235                 () -> RegExUtils.replaceFirst("any", "{badRegexSyntax}", ""),
236                 "RegExUtils.replaceFirst expecting PatternSyntaxException");
237     }
238 
239     @Test
240     public void testReplacePattern_StringStringString() {
241         assertNull(RegExUtils.replacePattern(null, "", ""));
242         assertEquals("any", RegExUtils.replacePattern("any", (String) null, ""));
243         assertEquals("any", RegExUtils.replacePattern("any", "", null));
244 
245         assertEquals("zzz", RegExUtils.replacePattern("", "", "zzz"));
246         assertEquals("zzz", RegExUtils.replacePattern("", ".*", "zzz"));
247         assertEquals("", RegExUtils.replacePattern("", ".+", "zzz"));
248 
249         assertEquals("z", RegExUtils.replacePattern("<__>\n<__>", "<.*>", "z"));
250         assertEquals("z", RegExUtils.replacePattern("<__>\\n<__>", "<.*>", "z"));
251         assertEquals("X", RegExUtils.replacePattern("<A>\nxy\n</A>", "<A>.*</A>", "X"));
252 
253         assertEquals("ABC___123", RegExUtils.replacePattern("ABCabc123", "[a-z]", "_"));
254         assertEquals("ABC_123", RegExUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", "_"));
255         assertEquals("ABC123", RegExUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", ""));
256         assertEquals("Lorem_ipsum_dolor_sit",
257                 RegExUtils.replacePattern("Lorem ipsum  dolor   sit", "( +)([a-z]+)", "_$2"));
258     }
259 
260 }