C++如何检查字符串包含特殊字符且其后存在指定字符?及邮箱输入框@与后续点号的验证实现
Hey there! Let's break down your two questions—they’re actually pretty closely related, so we can tackle them with similar logic. 😊
For this, we can use the built-in std::string methods from C++’s standard library—no fancy external tools needed. Here’s a straightforward approach:
- First, locate the position of your special character using
find(). - If the special character exists (i.e., the position isn’t
std::string::npos), confirm there’s space after it (so it’s not the last character in the string). - Then, search for your target character starting right after the special character. If that target is found, your condition is satisfied.
Here’s a code example to put this into action:
#include <iostream> #include <string> bool hasSpecialFollowedByTarget(const std::string& inputStr, char specialChar, char targetChar) { // Find the first occurrence of the special character size_t specialPosition = inputStr.find(specialChar); // Make sure the special character exists and isn't the last character if (specialPosition != std::string::npos && specialPosition < inputStr.size() - 1) { // Search for the target character starting right after the special one size_t targetPosition = inputStr.find(targetChar, specialPosition + 1); return targetPosition != std::string::npos; } // If any check fails, return false return false; } int main() { std::string test1 = "hello$world!foo"; std::string test2 = "hello$worldfoo"; // Check if '$' is followed by '!' std::cout << std::boolalpha << hasSpecialFollowedByTarget(test1, '$', '!') << "\n"; // Output: true std::cout << std::boolalpha << hasSpecialFollowedByTarget(test2, '$', '!') << "\n"; // Output: false return 0; }
As a beginner, you can start with simple string checks (similar to the first question) before moving to regex if you want more robust validation. Let’s cover both approaches:
Approach 1: Basic string checks (easy to understand)
This directly maps to your requirements:
- Check that
@exists and isn’t at the start/end of the input. - Check that there’s a
.somewhere after the@, and that the.isn’t immediately after@or at the end.
#include <iostream> #include <string> bool isValidEmail(const std::string& email) { size_t atPosition = email.find('@'); // First check: @ exists, isn't first or last character if (atPosition == std::string::npos || atPosition == 0 || atPosition == email.size() - 1) { return false; } // Second check: find a . after @, and it's not the last character size_t dotPosition = email.find('.', atPosition + 1); return dotPosition != std::string::npos && dotPosition < email.size() - 1; } int main() { std::string validEmail = "my.email@example.com"; std::string missingDot = "my.email@example"; std::string atStart = "@example.com"; std::string dotRightAfterAt = "my.email@.com"; std::cout << std::boolalpha << isValidEmail(validEmail) << "\n"; // true std::cout << std::boolalpha << isValidEmail(missingDot) << "\n"; // false std::cout << std::boolalpha << isValidEmail(atStart) << "\n"; // false std::cout << std::boolalpha << isValidEmail(dotRightAfterAt) << "\n"; // false return 0; }
Approach 2: Regex (more robust for edge cases)
If you want to handle more complex valid email formats (like allowing dots in the username or subdomains), C++11 and later have a <regex> library. Here’s a simple regex that meets your core requirements:
#include <iostream> #include <string> #include <regex> bool isValidEmailRegex(const std::string& email) { // Regex breakdown: // [a-zA-Z0-9._%+-]+ : Allowed characters for the username part // @ : Required @ symbol // [a-zA-Z0-9.-]+ : Domain name part // \.[a-zA-Z]{2,} : Required top-level domain (like .com, .org) with at least 2 characters std::regex emailPattern(R"([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})"); return std::regex_match(email, emailPattern); } int main() { std::string validEmail = "my.email+tag@subdomain.example.co.uk"; std::string invalidEmail = "my.email@example"; std::cout << std::boolalpha << isValidEmailRegex(validEmail) << "\n"; // true std::cout << std::boolalpha << isValidEmailRegex(invalidEmail) << "\n"; // false return 0; }
内容的提问来源于stack exchange,提问作者Dreamvillain

