In this part of my C++ tutorial I’ll start covering Regular Expressions. There are tons of examples and a problem for you to solve. I’ll cover how to search and then cycle through matches. We’ll look into cycling through matches with an iterator. Then we’ll create numerous regular expressions to get just that exact data we need and output or replace it.
All of the heavily commented code follows the video below. Feel free to do what ever you’d like with it.
If you like videos like this, consider donating $1, or simply turn off Ad Blocking software. Either helps me to continue making free tutorials.
Code From the Video
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
// ----- BEGINNING REGEX ----- void PrintMatches(std::string str, std::regex reg){ // Used when your searching a string std::smatch matches; // Show true and false in output std::cout << std::boolalpha; // Determines if there is a match and match // results are returned in matches while(std::regex_search(str, matches, reg)){ std::cout << "Checked for Results : " << matches.ready() << "\n"; std::cout << "Are there no matches : " << matches.empty() << "\n"; std::cout << "Number of matches : " << matches.size() << "\n"; // Get the first match std::cout << matches.str(1) << "\n"; // Eliminate the previous match and create // a new string to search str = matches.suffix().str(); std::cout << "\n"; } } // ----- END BEGINNING REGEX ----- // ----- ITERATOR REGEX ----- void PrintMatches2(std::string str, std::regex reg){ // This holds the first match std::sregex_iterator currentMatch(str.begin(), str.end(), reg); // Used to determine if there are any more matches std::sregex_iterator lastMatch; // While the current match doesn't equal the last while(currentMatch != lastMatch){ std::smatch match = *currentMatch; std::cout << match.str() << "\n"; currentMatch++; } std::cout << std::endl; } // ----- END ITERATOR REGEX ----- int main() { // ----- BEGINNING REGEX ----- std::string str = "The ape was at the apex"; // Create the pattern to search for which is // the letters ape followed maybe by anything // that is not a space std::regex reg ("(ape[^ ]?)"); PrintMatches(str,reg); // ----- END BEGINNING REGEX ----- // ----- ITERATOR REGEX ----- // Using an iterator with all matches std::string str2 = "I picked the pickle"; // Match (+) 1 or more of anything not a space std::regex reg2 ("(pick([^ ]+)?)"); PrintMatches2(str2,reg2); // ----- END ITERATOR REGEX ----- // ----- MATCH 1 OF SEVERAL LETTERS ----- std::string str3 = "Cat rat mat fat pat"; // Match any character in [] plus what follows std::regex reg3 ("([crmfp]at)"); PrintMatches2(str3,reg3); // We can also match characters in a range std::regex reg4("[C-Fc-f]at"); PrintMatches2(str3,reg4); // Use ^ to denote any character except for // those in brackets std::regex reg5("[^Cr]at"); PrintMatches2(str3,reg5); // ----- END MATCH 1 OF SEVERAL LETTERS ----- // ----- REPLACE MATCHES ----- std::string str6 = "Cat rat mat fat pat"; std::regex reg6("[Cr]at"); // Replace matches with Owl in the output std::string owlFood = std::regex_replace(str6,reg6,"Owl"); std::cout << owlFood << "\n"; // ----- END REPLACE MATCHES ----- // ----- MATCHING ANY CHARACTER ----- // . matches any character, but if we want to // search for a . we escape it with \ std::string str7 = "F.B.I. I.R.S. CIA"; std::regex reg7("([^ ]\..\..\.)"); PrintMatches2(str7,reg7); // ----- END MATCHING ANY CHARACTER ----- // ----- MATCHING WHITESPACE ----- // We can match any whitespace character std::string str8 = "This is a\n multiline string\n" "that has many lines"; std::regex reg8("\n"); std::string noLBStr = std::regex_replace(str8,reg8," "); std::cout << noLBStr << "\n"; // You can also replace // \b : Backspace // \f : Form Feed // \r : Carriage Return // \t : Tab // \v : Vertical Tab // ----- END MATCHING WHITESPACE ----- // ----- MATCHING SINGLE NUMBERS ----- // \d can be used instead of [0-9] // \D is the same as [^0-9] std::string str9 = "12345"; std::regex reg9("\\d"); PrintMatches2(str9, reg9); // You can also match within a range std::string str10 = "123 12345 123456 1234567"; // Match values that are between 5 and 7 digits std::regex reg10("\\d{5,7}"); PrintMatches2(str10, reg10); // ----- END MATCHING SINGLE NUMBERS ----- // ----- MATCHING ANY SINGLE LETTER OR NUMBER ----- // \w is the same as [a-zA-Z0-9_] // \W is the same as [^a-zA-Z0-9_] std::string str11 = "412-555-1212"; // Check if it is a phone number std::regex reg11("\\w{3}-\\w{3}-\\w{4}"); PrintMatches2(str11, reg11); // ----- END MATCHING ANY SINGLE LETTER OR NUMBER ----- // ----- MATCHING WHITESPACE ----- // \s is the same as [\f\n\r\t\v] // \S is the same as [^\f\n\r\t\v] std::string str12 = "Toshio Muramatsu"; // Check if its a valid name std::regex reg12("\\w{2,20}\\s\\w{2,20}"); PrintMatches2(str12, reg12); // ----- END MATCHING WHITESPACE ----- // ----- MATCHING ONE OR MORE ----- // Match a followed by 1 or more characters std::string str13 = "a as ape bug"; std::regex reg13("a[a-z]+"); PrintMatches2(str13, reg13); // ----- END MATCHING ONE OR MORE ----- // ----- PROBLEM ----- // Create a Regex that matches email addresses // from a list // 1. 1 to 20 lowercase and uppercase letters, // numbers, plus ._%+- // 2. An @ symbol // 3. 2 to 20 lowercase and uppercase letters, // numbers, plus .- // 4. A period // 5. 2 to 3 lowercase and uppercase letters std::string str14 = "db@aol.com m@.com @apple.com db@.com"; std::regex reg14("[\\w._%+-]{1,20}@[\\w.-]{2,20}.[A-Za-z]{2,3}"); PrintMatches2(str14, reg14); // ----- END PROBLEM ----- return 0; } |
Leave a Reply