Download presentation
Presentation is loading. Please wait.
Published byDella Tyler Modified over 8 years ago
1
Computer Science I Split. Regular Expressions Classwork: Trivia questions. Share. Show (stage 1) final project. Homework: work on final project.
2
Split (or splitTokens) and Join The split function provides a way to break up a string in pieces, such as phrase into words, based on a specific delimiter. – String produces array of strings. – The join function is the inverse The splitTokens function uses a set of delimiters.
3
Split String original = "Now is the time"; String[] words; void setup() { size(800,600); words = split(original," "); for (int i=0;i<words.length;i++) { println(words[i]); }
4
SplitTokens String original = "Now is the time."; String[] words; void setup() { size(800,600); words = splitTokens(original,"."); for (int i=0;i<words.length;i++) { println(words[i]); }
5
join String[] words = {"Larry", "Curly", "Moe"}; String combined; void setup() { size(800,700); combined = join(words,", "); textSize(35); fill(0); text(combined,100,100); }
6
Uses Join and split and splitTokens are ways to compress, perhaps for storage, and then decompress (decode) information. A string containing integers can be split and then the array converted to an integer array – Note: can combine this in one statement nums = int(split(original," ");
7
String original = "54 20 89 100"; String[] words; int[] nums; int sum = 0; void setup() { size(600,600); words = split(original," "); nums = int(words); for (int i=0;i<words.length;i++) { println(words[i]); sum = sum + nums[i]; } textSize(30); fill(0); text("The sum is "+str(sum), 10,50); }
8
Regular expressions Way to define a pattern – Determine if input matches the pattern – Can specify specify groups within the pattern Available in many programming languages – Details may vary! – Much available online Need to adapt to particular language Calculation (determination) will be fast[er] than can be programmed.
9
Regular expressions A language by itself. Evaluates strings of characters in an alphabet including upper and lower case letters, numbers and some symbols. (Chicken-and-egg problem) Here are some examples – Barack|Obama – [0-9]{3}-[0-9]{2}-[0-9]{4} which is the same as \\d{3}-\\d{2}-\\d{4} – ^Now.*party$
10
Character types So-called metacharacters are used to express certain patterns. The metacharacters are * \ ^ { } $. – ()|+?[] To use one of the metacharacters as itself, we do what is called escaping the character with \\ Be patient.
11
Basic combinations String of characters (by itself) matches instances of itself. The pipe indicates alternatives. – Hello|Goodbye Square brackets are used around set of characters or range to match any single instance. – [abc] or [A-C] or [1-6] Curly brackets used to indicate number of repetitions or range {3} or {3,5} ? Means 0 or 1 times, * means any number of times, + 1 or more times.. (dot) matches any single character. Anchors: ^ for start of string and $ for end of string. Use ^ within square brackets is negation: [^0-9] any character that is not a digit.
12
[QUICK] Classwork Divide the room. Look up regular expression for Social Security Number Zip code email
13
Processing The function match returns null if no match OR a String array. – 0 th element contains the whole string – If any groups, the 1 st indexed element returns the first group, the 2 nd indexed element returns the next, etc.
14
Example, using text input Need to build a tester sketch Use keyPressed function as shown previously. My example asks one question and does not allow you to ask again! Strings hold – Regular expression – Question – Responses for match and no match Functions – setup, keyPressed, draw – checkAnswer
15
Global variables String regex = "UConn|Connecticut|University of Connecticut"; String question= "What team is the Women's Basketball Champion? "; String myText = ""; String answer = ""; String responseCorrect= "Yes, University of Connecticut are the champs."; String responseWrong= "UConn were the favorites and they won."; Boolean asking = true;
16
void setup() { size(800,600); textSize(25); fill(0); //sets color of text } void keyPressed() { if (keyCode == BACKSPACE) { if (myText.length() > 0 ) { myText = myText.substring( 0, myText.length()- 1 ); } } else if (keyCode == DELETE) { myText = "" ; } else if (keyCode == ENTER) { answer = myText; myText = ""; } else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT) { myText = myText + key; }
17
void draw() { if (asking) { text(question+myText,10,10,width,height); if (answer.length()>0) { checkAnswer(regex,answer); asking = false; }
18
void checkAnswer(String reg, String guess) { String[] g; g = match(guess,reg); if (g!=null) { //don't care which item matched text(responseCorrect,10,100); } else { text(responseWrong,10,100); }
19
New question Just change some of the variables String regex = "^Barack Obama$|^Obama$"; String question= "Who is the President of the United States? "; String myText = ""; String answer = ""; String responseCorrect= "Yes, Barack Obama is the President."; String responseWrong="Wrong."; Boolean asking = true;
20
More complex test Check for matching and extract parts. Example: email String regex = "^([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+\\.[a-zA- Z]{2,4})$"; String question="What is your email? ";
21
void checkAnswer(String reg, String guess) { String[] g; g = match(guess,reg); if (g!=null) { //don't care which item matched text(responseCorrect,10,100); text(g[1]+" is your username.",10,200); text(g[2]+" is the domain.",10,300); } else { text(responseWrong,10,100); }
22
Classwork Working in pairs, think of 3-4 trivia questions and create regular expression. – Make it a question with more than one right answer – BUT, some answers that are wrong
23
Classwork Show your final project, probably not done.
24
Homework Keep working on final project.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.