COSC 1437 Strings and Regex
COSC 1437 Strings and Regex
:: 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]]
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.
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)
<<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:
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.
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:
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.
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:
Objective 4) Write a method called twoNum that uses a regular expression that checks if a string contains exactly 2
numbers
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.
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”.
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.