You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++如何检查字符串包含特殊字符且其后存在指定字符?及邮箱输入框@与后续点号的验证实现

Hey there! Let's break down your two questions—they’re actually pretty closely related, so we can tackle them with similar logic. 😊

Question 1: Check if a string contains a special character followed by a specific character in C++

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:

  1. First, locate the position of your special character using find().
  2. 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).
  3. 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;
}
Question 2: Email input validation (ensure @ exists and has a . after it)

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 14:57:49