Jump to content
  • Advertisement
Sign in to follow this  

Regex - Scan a string for special chars

This topic is 997 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


In my program I have a field, where the user can input data, that gets stored after it. This field should only accept normal letters, digits and (German) umlauts. For the first 2  things I could already create a Regex, but I don't know, how I can add the third thing (ä,ü,ö,Ä,Ü,Ö).

Also I'm not exactly sure, how I can check, if the regex is in the string, or not. My plan is, that I scan the string (with regex_match) and if there is more than one char, that is not from one of the 3, at the top, mentioned categories, it should return false, otherwise it should return true.



std::regex reg("[^A-Za-z0-9\s]"); // Add Umlauts
std::string testString1 = "Hello, this is the first test string"
std::string testString2 = "Hällo, this is the 2nd test String+#"

// Check testString1 for reg
//-> Should return true

// Check testString2 for reg
//-> Should return false

Detailed Question

How can I check, if a string matches the reg in my example and how can I extend the reg, that it allows umlauts?



Share this post

Link to post
Share on other sites

Normally you try to write a regular expression that fully captures the kind of strings you want to allow. Your approach of having a pattern you want to disallow and search for instances of it can also work, but would be a less common approach.


To simplify, I'm going to imagine a case you only want lowercase letters (e.g. [a-z]). You can say that you expect zero or more, or one or more, by using the metacharacters * or +. You can use the metacharacters ^ and $ to say that your requirement is to match against the entire input, not just a partial match in the middle.


So that might yield ^[a-z]*$, which means a string starting with zero or more lowercase characters and then ending.


You might have something like this:

#include <regex>
#include <string>
#include <iostream>
int main() {
#if 0
    std::regex reg("^[a-z]+$");
    std::regex reg("^[a-z]*$");
    std::string strings[] = { "", "    ", "hello", "1number2", "!goodbye!" };
    for (std::string s : strings) {
        std::cout << '\"' << s << "\": " << (std::regex_match(s, reg) ? "matches" : "doesn\'t match") << '\n';


There are helpful sites which can speed up developing a regular expressions, the one I linked is tailored for Ruby (regex syntax can vary), but there are lots of alternatives out there.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!