Skip to content

Commit 95fbe80

Browse files
committed
112 Create skeleton for regex
1 parent 0767646 commit 95fbe80

File tree

7 files changed

+625
-0
lines changed

7 files changed

+625
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Crazy Regex
2+
3+
### Objectives
4+
You're supposed to know how to work regex and be able to build Patterns and Matchers
5+
6+
### Regular expressions - sequence of characters that define a search pattern for text
7+
8+
---
9+
10+
There 2 peace pf puzzle:
11+
* Literal characters - I want to match literally the character I specified (like 'a')
12+
* Meta characters - I want to match any character of this kind (more generic/abstract thing)
13+
14+
Single char
15+
16+
* \\d -> 0-9
17+
* \\D -> negate of \\d
18+
* \\w -> A-Za-z0-9
19+
* \\W -> negate of \\w
20+
* \\s -> whitespace, tab
21+
* \\S -> negate of \\s
22+
* . -> anything but newline
23+
* \\. -> literal dot
24+
25+
26+
Quantifiers - modify single characters how many of them you want match in a row
27+
* \* -> Occurs zero or more times
28+
* \+ -> 1 or more
29+
* ? -> zero or one
30+
* {min, max} -> some range
31+
* {n} -> precise quantity
32+
33+
34+
Position
35+
* ^ -> beginning
36+
* $ -> end
37+
* \\b -> word boundary
38+
39+
---
40+
41+
Character class -> is the thing that appears in between []. For example [abc] -> match 'a' or 'b' or 'c'.
42+
Another example [-.] -> match dash or period. Here . is not meta character anymore and ^ are special characters inside []
43+
* [0-5] -> match all numbers from 0 to 5. [^0-5] -> match anything that NOT 0-5
44+
BUT it works like meta character only when it on first position, otherwise - its literal, [a^bc] - like this
45+
46+
---
47+
48+
Capturing Groups - whenever u do regex search it matches whole result as a group 0.
49+
* \\d{3}-\\d{3}-\\d{4} -> 212-555-1234 = GROUP 0
50+
51+
Parentheses can capture a subgroup:
52+
\\d{3}-(\\d{3})-(\\d{4}) where 212-555-1234 = GROUP 0, 555 = GROUP 1, 1234 = GROUP 2
53+
54+
We can refer to this groups by $1 ($ when we want to replace) and \1 (within regex itself referring to capture group
55+
it's called back reference)
56+
57+
---
58+
59+
#### 🆕 First time here? – [See Introduction](https://github.com/bobocode-projects/java-fundamentals-course/tree/main/0-0-intro#introduction)
60+
#### ➡️ Have any feedback? – [Please fill the form ](https://forms.gle/jhXEAzG4TB81S43CA)
61+
62+
63+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>3-0-java-core</artifactId>
7+
<groupId>com.bobocode</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>3-6-3-crazy-regex</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>11</maven.compiler.source>
16+
<maven.compiler.target>11</maven.compiler.target>
17+
</properties>
18+
19+
</project>
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
package com.bobocode.se;
2+
3+
import com.bobocode.util.ExerciseNotCompletedException;
4+
5+
import java.util.regex.Pattern;
6+
7+
/**
8+
* {@link CrazyRegex} is an exercise class. Each method returns Pattern class which
9+
* should be created using regex expression. Every method that is not implemented yet
10+
* throws {@link ExerciseNotCompletedException}
11+
* TODO: remove exception and implement each method of this class using java.util.regex.Pattern
12+
*/
13+
public class CrazyRegex {
14+
15+
/**
16+
* Create Pattern that accepts String with regex inside and can
17+
* find all words "Curiosity" in text
18+
*
19+
* @return Pattern with regex expression
20+
*/
21+
public Pattern findSpecificWord() {
22+
throw new ExerciseNotCompletedException();
23+
}
24+
25+
/**
26+
* Create Pattern that accepts String with regex inside and can
27+
* find first word in text
28+
*
29+
* @return Pattern with regex expression
30+
*/
31+
public Pattern findFirstWord() {
32+
throw new ExerciseNotCompletedException();
33+
}
34+
35+
/**
36+
* Create Pattern that accepts String with regex inside and can
37+
* find last word in text
38+
*
39+
* @return Pattern with regex expression
40+
*/
41+
public Pattern findLastWord() {
42+
throw new ExerciseNotCompletedException();
43+
}
44+
45+
/**
46+
* Create Pattern that accepts String with regex inside and can
47+
* find all numbers in text
48+
*
49+
* @return Pattern with regex expression
50+
*/
51+
public Pattern findAllNumbers() {
52+
throw new ExerciseNotCompletedException();
53+
}
54+
55+
/**
56+
* Create Pattern that accepts String with regex inside and can
57+
* can find all dates like: 1971-11-23
58+
*
59+
* @return Pattern with regex expression
60+
*/
61+
public Pattern findDates() {
62+
throw new ExerciseNotCompletedException();
63+
}
64+
65+
/**
66+
* Create Pattern that accepts String with regex inside and can
67+
* different variations of word 'color'. We are looking for:
68+
* 'color', 'colour', 'colors', 'colours'
69+
*
70+
* @return Pattern with regex expression
71+
*/
72+
public Pattern findDifferentSpellingsOfColor() {
73+
throw new ExerciseNotCompletedException();
74+
}
75+
76+
/**
77+
* Create Pattern that accepts String with regex inside and can
78+
* can find all zip codes in text
79+
*
80+
* @return Pattern with regex expression
81+
*/
82+
public Pattern findZipCodes() {
83+
throw new ExerciseNotCompletedException();
84+
}
85+
86+
/**
87+
* Create Pattern that accepts String with regex inside and can
88+
* find different variations of word 'link', We are looking for:
89+
* 'lynk', 'link', 'l nk', 'l(nk'
90+
*
91+
* @return Pattern with regex expression
92+
*/
93+
public Pattern findDifferentSpellingsOfLink() {
94+
throw new ExerciseNotCompletedException();
95+
}
96+
97+
/**
98+
* Create Pattern that accepts String with regex inside and can
99+
* find phone number. For example: 555-555-5555
100+
*
101+
* @return Pattern with regex expression
102+
*/
103+
public Pattern findSimplePhoneNumber() {
104+
throw new ExerciseNotCompletedException();
105+
}
106+
107+
/**
108+
* Create Pattern that accepts String with regex inside and can
109+
* can find numbers with following requirements:
110+
* - inside the number can be only digits from 0 to 5
111+
* - length 3
112+
*
113+
* @return Pattern with regex expression
114+
*/
115+
public Pattern findNumbersFromZeroToFiveWithLengthThree() {
116+
throw new ExerciseNotCompletedException();
117+
}
118+
119+
/**
120+
* Create Pattern that accepts String with regex inside and can
121+
* find all words in text that have length 5
122+
*
123+
* @return Pattern with regex expression
124+
*/
125+
public Pattern findAllWordsWithFiveLength() {
126+
throw new ExerciseNotCompletedException();
127+
}
128+
129+
/**
130+
* Create Pattern that accepts String with regex inside and can
131+
* find words and numbers with following constraints:
132+
* - not shorter than two symbols
133+
* - not longer than three symbols
134+
*
135+
* @return Pattern with regex expression
136+
*/
137+
public Pattern findAllLettersAndDigitsWithLengthThree() {
138+
throw new ExerciseNotCompletedException();
139+
}
140+
141+
/**
142+
* Create Pattern that accepts String with regex inside and can
143+
* find all words that begin with capital letter
144+
*
145+
* @return Pattern with regex expression
146+
*/
147+
public Pattern findAllWordsWhichBeginWithCapitalLetter() {
148+
throw new ExerciseNotCompletedException();
149+
}
150+
151+
/**
152+
* Create Pattern that accepts String with regex inside and can
153+
* find only the following abbreviation:
154+
* - AK, AL, AR, AZ, CA, CO, CT, PR, PA, PD
155+
*
156+
* @return Pattern with regex expression
157+
*/
158+
public Pattern findAbbreviation() {
159+
throw new ExerciseNotCompletedException();
160+
}
161+
162+
/**
163+
* Create Pattern that accepts String with regex inside and can
164+
* find all open braces
165+
*
166+
* @return Pattern with regex expression
167+
*/
168+
public Pattern findAllOpenBraces() {
169+
throw new ExerciseNotCompletedException();
170+
}
171+
172+
/**
173+
* Create Pattern that accepts String with regex inside and can
174+
* find everything inside []
175+
*
176+
* @return Pattern with regex expression
177+
*/
178+
public Pattern findOnlyResources() {
179+
throw new ExerciseNotCompletedException();
180+
}
181+
182+
/**
183+
* Create Pattern that accepts String with regex inside and can
184+
* find all http links inside ()
185+
*
186+
* @return Pattern with regex expression
187+
*/
188+
public Pattern findOnlyLinksInNote() {
189+
throw new ExerciseNotCompletedException();
190+
}
191+
192+
/**
193+
* Create Pattern that accepts String with regex inside and can
194+
* all http links
195+
*
196+
* @return Pattern with regex expression
197+
*/
198+
public Pattern findOnlyLinksInJson() {
199+
throw new ExerciseNotCompletedException();
200+
}
201+
202+
/**
203+
* Create Pattern that accepts String with regex inside and can
204+
* find all .com, .net and .edu emails
205+
*
206+
* @return Pattern with regex expression
207+
*/
208+
public Pattern findAllEmails() {
209+
throw new ExerciseNotCompletedException();
210+
}
211+
212+
/**
213+
* Create Pattern that accepts String with regex inside and can
214+
* can find the following examples of phone numbers:
215+
* - 555-555-5555
216+
* - 555.555.5555
217+
* - (555)555-5555
218+
*
219+
* @return Pattern with regex expression
220+
*/
221+
public Pattern findAllPatternsForPhoneNumbers() {
222+
throw new ExerciseNotCompletedException();
223+
}
224+
225+
/**
226+
* Create Pattern that accepts String with regex inside and can
227+
* find only duplicates
228+
*
229+
* @return Pattern with regex expression
230+
*/
231+
public Pattern findOnlyDuplicates() {
232+
throw new ExerciseNotCompletedException();
233+
}
234+
235+
/**
236+
* You have a text where all names recorded as first name, last name.
237+
* Create matcher and use method replaceAll to record that names as:
238+
* - last name first name
239+
*
240+
* @return String where all names recorded as last name first name
241+
*/
242+
public String replaceFirstAndLastNames(String names) {
243+
throw new ExerciseNotCompletedException();
244+
}
245+
246+
/**
247+
* You have a text with phone numbers.
248+
* Create matcher and use method replaceAll to replace last digits:
249+
* - 555-XXX-XXXX
250+
*
251+
* @return String where in all phone numbers last 7 digits replaced to X
252+
*/
253+
public String replaceLastSevenDigitsOfPhoneNumberToX(String phones) {
254+
throw new ExerciseNotCompletedException();
255+
}
256+
257+
/**
258+
* You have a text with resources and links to those resources:
259+
* - [Bobocode](https://www.bobocode.com)
260+
* Create matcher and use method replaceAll to get the following result:
261+
* - <a href="https://www.bobocode.com">Bobocode</a>
262+
*
263+
* @return String where all resources embraced in href
264+
*/
265+
public String insertLinksAndResourcesIntoHref(String links) {
266+
throw new ExerciseNotCompletedException();
267+
}
268+
}

0 commit comments

Comments
 (0)