Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Lasse Myllärinen Exercise 1 (2)

16.2.2022
Haaga-Helia University of Applied Sciences
Programming 1

Answers

A. Write all the strings that match the Java regular expression patterns below;
1) String “1” String “2”
2) String “11”
3) String ”11” String “111”
4) String “121212”
5) String “121212”
6) String “cart”
7) String “cab” String “car” String “cat”
8) String “cab” String “cart”
9) String “Yum” String ”YumYum”
10) Strings “” “yum” “Yum” “yummy” “Yummy”
11) Strings “ban” “can” “dan” “ean” “fan” “man” “van”
12) Strings with digits between “-0” to “-59”
13) Cello
14) \\

B. How do the regex patterns below differ from each other?

matches only with character combo => String; 1any single character be
sides linebreak5

a single character matches with; 1 or any character besides linebreak or 5

matches only with character combo => String; 1.5

C. Does the Java regular expression pattern below match any of the given strings when we use
the match method of the String class?

Regex String Match


“Hello” “Hello, Joe!” No

“Hello,{0,2}Joe!” “Joe” No
“Joe!” No
“Hello, Joe!” No
“Hello Joe!” No
“Hello,Joe!” Yes
“Hello,Hello,Joe!” No

D. Does the Java regular expression pattern below match any of the given strings when we
use the match method of the String class?

Regex as a String literal Matching String


“Hello\\S{1,}” Non

“Hello\\D{1,}” “Hello, Joe!”

“.{0,}Joe.{0,}” “Joe”
“Hello, Joe!”
Lasse Myllärinen Exercise 2 (2)

16.2.2022
Haaga-Helia University of Applied Sciences
Programming 1

“Hello, Joeseph!”

“.{0,}\\.class” “RegExExample.class”
“.class”

E. Write the regex patterns below using the { } quantifier notation.

2{1,}2=4
2{0,}2=4

You might also like