0% found this document useful (0 votes)
30 views

SWD02 Regular Expressions in Java in Class Exercise

This document appears to be an assignment or exercise on regular expressions from a programming course. It contains 5 sections with questions on: 1) Matching strings to regular expression patterns 2) Differences between regex patterns 3) Whether regex patterns match given strings 4) Whether additional regex patterns match strings 5) Writing regex patterns using quantifier notation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

SWD02 Regular Expressions in Java in Class Exercise

This document appears to be an assignment or exercise on regular expressions from a programming course. It contains 5 sections with questions on: 1) Matching strings to regular expression patterns 2) Differences between regex patterns 3) Whether regex patterns match given strings 4) Whether additional regex patterns match strings 5) Writing regex patterns using quantifier notation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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