# regex

***

**1. Email Validation**

```cpp
#include <regex>

bool IsValidEmail(const std::string& email) {
  std::regex pattern(
      "^\\s*[a-zA-Z0-9_\\.\\+\\-]+@[a-zA-Z0-9\\.\\-]+\.[a-zA-Z0-9]{2,}\\s*$");
  return std::regex_match(email, pattern);
}
```

**2. Phone Number Validation**

```cpp
#include <regex>

bool IsValidPhoneNumber(const std::string& phone) {
  std::regex pattern(
      "^(\\+\\d{1,2}\\s?)?((\\d{3}\\s?)|(\\d{2,3}-?)|\\d{2,3})\\s?(\\d{3}-?)"
      "?\\s?(\\d{4}|\\d{3}-?)\\s?(\\d{4}|\\d{3}-?)\\s*$");
  return std::regex_match(phone, pattern);
}
```

**3. Password Strength Validation**

```cpp
#include <regex>

bool IsStrongPassword(const std::string& password) {
  std::regex pattern("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*#?&])[A-Za-z\\d@$!%*#?&]{8,}$");
  return std::regex_match(password, pattern);
}
```

**4. URL Validation**

```cpp
#include <regex>

bool IsValidUrl(const std::string& url) {
  std::regex pattern("^(http|https):\\/\\/([\\w\\-]+\\.)+[\\w\\-]+([\\w\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])?$");
  return std::regex_match(url, pattern);
}
```

**5. IP Address Validation**

```cpp
#include <regex>

bool IsValidIpAddress(const std::string& ip) {
  std::regex pattern("^(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})$");
  return std::regex_match(ip, pattern);
}
```

**6. HTML Tag Removal**

```cpp
#include <regex>

std::string RemoveHtmlTags(const std::string& html) {
  std::regex pattern("<[^>]*>");
  return std::regex_replace(html, pattern, "");
}
```

**7. Date Extraction from Text**

```cpp
#include <regex>

std::string ExtractDate(const std::string& text) {
  std::regex pattern("(\\d{1,2})/(\\d{1,2})/(\\d{4})");
  std::smatch match;
  if (std::regex_search(text, match, pattern)) {
    return match[0];
  }
  return "";
}
```

**8. Find and Replace Text**

```cpp
#include <regex>

std::string FindAndReplace(const std::string& str,
                           const std::string& find,
                           const std::string& replace) {
  std::regex pattern(find);
  return std::regex_replace(str, pattern, replace);
}
```

**9. Split String by Delimiter**

```cpp
#include <regex>

std::vector<std::string> Split(const std::string& str, const std::string& delimiter) {
  std::regex pattern(delimiter);
  return std::vector<std::string>(std::sregex_token_iterator(str.begin(), str.end(), pattern, -1),
                                   std::sregex_token_iterator());
}
```

**10. Check for White Space Only**

```cpp
#include <regex>

bool IsWhiteSpaceOnly(const std::string& str) {
  std::regex pattern("^\\s*$");
  return std::regex_match(str, pattern);
}
```

**11. Find and Match Whole Words**

```cpp
#include <regex>

bool IsWholeWordMatch(const std::string& str, const std::string& word) {
  std::regex pattern("\\b" + word + "\\b");
  return std::regex_search(str, pattern);
}
```

**12. Match Multiple Patterns**

```cpp
#include <regex>

bool MatchesAnyPattern(const std::string& str, const std::vector<std::string>& patterns) {
  for (const auto& pattern : patterns) {
    if (std::regex_match(str, std::regex(pattern))) {
      return true;
    }
  }
  return false;
}
```

**13. Match Using Negative Lookahead**

```cpp
#include <regex>

bool DoesNotContain(const std::string& str, const std::string& unwanted) {
  std::regex pattern("(?!.*" + unwanted + ".*).+");
  return std::regex_match(str, pattern);
}
```

**14. Match Using Positive Lookahead**

```cpp
#include <regex>

bool Contains(const std::string& str, const std::string& wanted) {
  std::regex pattern(".*" + wanted + ".*");
  return std::regex_match(str, pattern);
}
```

**15. Match Using Grouping**

```cpp
#include <regex>

std::string GroupMatch(const std::string& str, const std::string& pattern) {
  std::regex regex(pattern);
  std::smatch match;
  if (std::regex_search(str, match, regex)) {
    return match[1];
  }
  return "";
}
```

**16. Replace Using Grouping**

```cpp
#include <regex>

std::string ReplaceUsingGroup(const std::string& str, const std::string& pattern, const std::string& replace) {
  std::regex regex(pattern);
  return std::regex_replace(str, regex, replace, std::regex_constants::format_first_only);
}
```

**17. Tokenize Using Regex**

```cpp
#include <regex>
#include <iterator>

std::vector<std::string> TokenizeRegex(const std::string& str, const std::regex& pattern) {
  std::vector<std::string> tokens;
  std::regex_token_iterator<std::string::iterator> begin(str.begin(), str.end(), pattern);
  std::regex_token_iterator<std::string::iterator> end;
  std::copy(begin, end, std::back_inserter(tokens));
  return tokens;
}
```

**18. Find and Replace Using Iterators**

```cpp
#include <regex>
#include <iterator>

std::string FindAndReplaceUsingIterators(const std::string& str,
                                          const std::string& find,
                                          const std::string& replace) {
  std::regex regex(find);
  std::string result;
  std::regex_replace(std::back_inserter(result), str.begin(), str.end(), regex, replace);
  return result;
}
```

**19. Check for Palindrome**

```cpp
#include <regex>

bool IsPalindrome(const std::string& str) {
  std::regex pattern("(.*)[\\W_]+\\1");
  return std::regex_match(str, pattern);
}
```

**20. Check for SSN**

```cpp
#include <regex>

bool IsValidSSN(const std::string& ssn) {
  std::regex pattern("^\\d{3}-\\d{2}-\\d{4}$");
  return std::regex_match(ssn, pattern);
}
```

**21. Check for Credit Card Number**

```cpp
#include <regex>

bool IsValidCreditCardNumber(const std::string& cc) {
  std::regex pattern("^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9]{2})[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11})$");
  return std::regex_match(cc, pattern);
}
```

**22. Check for Bitcoin Address**

```cpp
#include <regex>

bool IsValidBitcoinAddress(const std::string& address) {
  std::regex pattern("^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$");
  return std::regex_match(address, pattern);
}
```

**23. Check for MAC Address**

```cpp
#include <regex>

bool IsValidMacAddress(const std::string& mac) {
  std::regex pattern("^[0-9a-f]{2}([-:]?)[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$");
  return std::regex_match(mac, pattern);
}
```

**24. Check for IPv6 Address**

```cpp
#include <regex>

bool IsValidIpv6Address(const std::string& ip) {
  std::regex pattern("^(?:[0-9a-f]{1,4}:){7}[0-9a-f]{1,4}$");
  return std::regex_match(ip, pattern);
}
```

**25. Check for File Extension**

```cpp
#include <regex>

std::string GetFileExtension(const std::string& filename) {
  std::regex pattern(".*\\.(.*)$");
  std::smatch match;
  if (std::regex_search(filename, match, pattern)) {
    return match[1];
  }
  return "";
}
```

**26. Remove Leading and Trailing Whitespace**

```cpp
#include <regex>

std::string TrimWhitespace(const std::string& str) {
  std::regex pattern("^\\s+|\\s+$");
  return std::regex_replace(str, pattern, "");
}
```

**27. Check for Valid Time**

```cpp
#include <regex>

bool IsValidTime(const std::string& time) {
  std::regex pattern("^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$");
  return std::regex_match(time, pattern);
}
```

**28. Check for Strong Password**

```cpp
#include <regex>

bool IsStrongPassword(const std::string& password) {
  std::regex pattern("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#

```
