There is more to write about the usage of regular expressions than I wrote in my last post The Regular Expression Library. Let’s continue.
The text determines the regular expression, the result, and the capture groups
First of all, the type of text determines the character type of the regular expression, the type of the search result, and the type of the capture group. Of course, my argument also holds if other parts of the regex machinery are applied to text. Okay, that sounds worse than it is. Capture is a subexpression in your search result, which you can define in round braces. I wrote already about it in my last post The Regular Expression Library.
The table gives all the types depending on the text type.
Here is an example of all the variations of std::regex_search depending on the text type.
First, I used a const char*, a std::string, a const wchar_t*, and finally, a std::wstring as text. Because it is almost the same code in the four variations, from now on and for the rest of this post, I will only refer to the std::string.
The text contains a substring that stands for a time expression. Thanks to the regular expression “(?[0-9]|2[0-3]):[0-5][0-9]“, I can search for it. The regular expression defines a time format consisting of an hour and a minute, separated by a colon. Here is the hour and minute part:
- hour: ?[0-9]|2[0-3]:
- ?: 0 or 1 (optional)
- [0-9]: a number from 0 to 9
- |: stands for or
- 2[0-3]: 2 followed by a number from 0 to 3
- minute: [0-5][0-9]: a number from 0 to 5 followed by a number from 0 to 9
Finally, the output of the program.
Use regex_iterator or regex_token_iterator for repeated search
Don’t repeat std::search calls because you can quickly lose word boundaries or have empty hits. Use std::regex_iterator or std::regex_token_iterator for repeated search instead. std::regex_token_iterator allows you to address each capture group’s components or the text between the matches.
The “Hello World” of repeated search with regex is to count how often a word appears in a text. Here is the corresponding program.
To better understand the program, I added a few comments to it.
The using declarations in line 1 help me to type less. The function wordCount determines the frequency of each word, and the function frequencyOfWords return to each frequency of all words. What is a word? Line 2 defines it with the regular expression, and line 3 uses it in a std::sregex_iterator. Let’s see which answers I can give with the two functions.
- Line 4: first 20 (key, value)-pairs
- Line 5: frequency of the words “Web” and “The”
- Line 6: number of unique words
- Line 7: number of frequencies
- Line 8: all appearing frequencies
- Line 9: the most frequently used word
- Line 10: words that appear more than 1000 times
Now, I need a lengthy text. Of course, I will use Grimm’s fairy tales from the project Gutenberg . Here is the output:
I’m almost done with the regex functionality in C++, but I have one guideline in mind which makes repeated search often easier: Search not for the text patterns but the delimiters of the text patterns. I call this a negative search.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, and Marco Parri Empoli.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,