Skip to content

Commit 4a111c1

Browse files
authored
Merge pull request #118 from bobocode-projects/112-crazy-regex-exercise
112 Create skeleton for regex
2 parents 8a799b1 + 061a34f commit 4a111c1

File tree

7 files changed

+615
-0
lines changed

7 files changed

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

0 commit comments

Comments
 (0)