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

COSC 1437 Strings and Regex

The document provides objectives and examples for a string and regex lab involving: 1) Adding decoration text and variables to a conversation system using string and regex methods 2) Writing regex methods to check for affirmative words, consonant-vowel-consonant patterns, phone numbers, numbers of characters, and password requirements 3) Checking for duplicate words using regex

Uploaded by

Brayan Rojas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

COSC 1437 Strings and Regex

The document provides objectives and examples for a string and regex lab involving: 1) Adding decoration text and variables to a conversation system using string and regex methods 2) Writing regex methods to check for affirmative words, consonant-vowel-consonant patterns, phone numbers, numbers of characters, and password requirements 3) Checking for duplicate words using regex

Uploaded by

Brayan Rojas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

COSC 1437

String and Regex Lab


Part 1: Strings and String Methods in Java:
Take the given example code which is part of a simple conversation/messaging system. Review the code to make sure
you are familiar with the String methods that it is currently using. We are going to be adding more functionality to it.
Currently, we have Topics, that consist of a title, some content, and some options. The user is shown the content and
then the options allow the user to respond. Those responses then link to another topic.

:: Yes
Thank you stranger, we are lost in the woods. Can you help us find the way out?
[[Okay I can help]]
[[Find your own way]]

Objective 1: Adding decoration text


So right now what the user’s response options are exactly what topic it goes to. We need to have some way of saying
one thing that goes to another named topic. It would be really annoying to have a player option say 3 or 4 sentences and
then have to name a topic those same 3 or 4 sentences. Instead lets do something like:

[[Okay I can do that-> Accept]]

Now what it shows to the user is the text at the beginning but if that option is selected it will go to the Accept topic.

Objective 2: Adding variables


We will be adding a way to set variables based on where the user goes in the conversation. The format for how variables
will be set should look something like:

<<set $AcceptMission = 1>>

We will be focusing on integer (whole numbers) and string datatypes. Floating or any other datatypes are not required
to be implemented. I typically do not consider capitalization for either the command (set) or the variable name but I will
leave that option up to you. You can choose however you want to store/save the variables for later, but keep in mind
that later we will need to retrieve them.
Bonus 5-10%: include some basic math for the integer type variables (make sure you indicate this on the submission
page if you want credit)

Objective 3: Getting Variables


Now we need to add the ability to retrieve variables. So we will add a get command that looks something like:

<<get $AcceptMission>>

This doesn’t have a lot of utility but will be used in the next objective. It could be used to say count the number of times
you’ve done stuff (if some simple math was added to the variables to allow them to count). This could also be used to
store names like storing the player name and getting it whenever another player said hello for instance:

:: Greet
Hello <<get $PlayerName>>. How are you doing
[[Well I could be better -> Okay]]
COSC 1437

Objective 4:
The last objective is to add a conditional statement. I’m fine with you guys working out the format of this command a bit
for yourself but this is how I do it:

<<if <<get $AcceptMission>> = 1>>Since you are helping us how about I cheer you on!<<endif>>

So conditionals do nothing if you aren’t getting variables (though if you wanted to make the if statement automatically
get variables without having to use the get command that would work too). And the goal of the if statement is to check
a variable to either open up content text (like in the example above) or open up player options (that would take you to
other topics).

<<if <<get $AcceptMission>> = 1>>[[Your cheers are greatly appreciated -> Yes]]<<endif>>

This example shows a player option that is only available if the AcceptMission variable is set to 1. I also typically
abbreviate this as:

<<if $AcceptMission>>[[Your cheers are greatly appreciated -> Yes]]<<endif>>

Since I often want to know just if a variable exists (note this does automatically use the get command). One final note is
that I end all of my if statements with an <<endif>> this is because text could be multiple lines long. The endif is a clear
indication of when I’m through with the if statement.

Part 2: Regular Expressions:


See the notes for regular expressions before starting these problems. Each of these methods is supposed to be done
with only regular expressions. If you need to split up the expression into more than one regular expression that is okay,
though typically there is one single expression that will solve each problem.

The basic form is that there should be a method that will return true or false if any of the regular expressions you write
work:

public static boolean MethodName(String s)


{
return s.matches("Your Regular Expression Goes Here");
}

Objective 1) Write a method called affirmative which takes a string and returns true if the string contains “True” or
“true” or “Yes” or “yes”;

Objective 2) Write a method conVolCon which will check if that string input as a parameter has a consonant followed by
a vowel followed by a consonant pattern. This method should ignore any characters after the first 3.

String test = "fox"; //true


String test = "Cat"; //true
String test = "leaf”; //false
String test = "owl”; //false
String test = "daddy”; //true

Objective 3) Write a method called checkPhoneFormat which takes a string as a parameter and uses a regular
expression to check whether the string matches the following phone format. All other formats should return false:

String test = "(555) 444 3477"; //true


String test = "(111) 111 1121"; //true
COSC 1437

Objective 4) Write a method called twoNum that uses a regular expression that checks if a string contains exactly 2
numbers

String test = "25”; //true


String test = "a2bc5a”; //true
String test = "a2b33c5a”; //false

Objective 5) Write a method called PasswordValidate, which will check a string to confirm that it contains at least 1
number, at least 1 uppercase letter, at least 1 special symbol (!@#$%^&*), and is at least 4 characters long (should be
done with regex not with String.length)

As a starting point this expression will tell if a string contains at least one digit: ^(?=.*\\d).*$
The ^ starts from the beginning of the string, the ?= searches ahead for any character that is a digit and then the .* says
ignore the rest of the string.

String test = "12312&A”; //true


String test = "a2bc5a”; //false – missing the symbol

Objective 6) The following Regular expression checks if two words in a string (separated by a number of whitespaces)
are duplicate words. The following code will output “True”.

String testThis = "test test"; //true


if (help.matches("\\b(\\w+)\\s+\\1\\b"))
System.out.println("True");
else
System.out.println("False");

Your job is to take the code above and make it return true for strings with 3 consecutive duplicate words. For example,
your regular expression should return true for the following string.

String fun = "fun fun fun";

You might also like