C++ regex

使用C++regex判断数字,实数,ip,电子邮件,单词,电话号,日期等格式

#include "check.h"
#include <regex>
#include <string>

using namespace std;
///判断全为数字
bool all_digit(const string &s)
{
   regex r("^[0-9]*$");
   return regex_match(s,r);
}
///判断单词
bool all_alpha(const string &s)
{
   regex r("^[[:alpha:]]*$");
   return regex_match(s,r);
}
///判断全为单词或数字
bool all_alnum(const string &s)
{
   regex r("^[[:alnum:]]*$");
   return regex_match(s,r);
}
///判断整数
bool is_int(const string &s)
{
   regex r("^[-]?(0|([1-9][0-9]*))$");
   return regex_match(s,r);
}
///判断实数
bool is_float(const string &s)
{
   regex r("^-?(0|([1-9][0-9]*))\\.[0-9]+$");
   return regex_match(s,r);
}
///判断ip
bool is_ip(const string &s)
{
   regex r("^((((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|(([1-9][0-9]?)|0))\\.){3}"
                        "((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|(([1-9][0-9]?)|0)))$");
   return regex_match(s,r);
}
///判断电话号,以0或1开头,11位数字组成
bool is_phone(const string &s)
{
   regex r("^[01][0-9]{10}$");
   return regex_match(s,r);
}
///判断日期1900-2999年之间
bool is_date(const string &s)
{
   regex r("^[12][0-9]{3}-((0[1-9])|(1[0-2]))-((0[1-9])|([12][0-9])|(3[01]))$");
   return regex_match(s,r);
}
///判断电子邮箱地址
bool is_mail(const string &s)
{
  regex r("^[[:alnum:]][email protected][[:alnum:]]+\\.[a-z]+$");
  return regex_match(s,r);
}
时间: 2024-07-30 10:07:55

C++ regex的相关文章

Java Regex match IP address

Reference: [1] https://www.mkyong.com/regular-expressions/how-to-validate-ip-address-with-regular-expression/ import java.util.regex.Matcher; import java.util.regex.Pattern; public class IPAddressValidator{ private Pattern pattern; private Matcher ma

java.util.regex.PatternSyntaxException: Dangling meta character &#39;*&#39; near index 0

使用repalceAll 方法出现java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0异常 代码如下: 1 @Test 2 public void testReplaceAll(){ 3 String sql = "select * from per_handle where id not in('3ce7405509414105a65e7456987e7393')"; 4 St

linux find -regex 使用正则表达式

find之强大毋庸置疑,此处只是带领大家一窥find门径,更详细的说明见man  find和 info find.整篇文章循序渐进,从最常用的文件名测试项开始步步深入,到第六节基本讲完find处理文件的规则,再之后的章节是一些常用表达式的说明. (此篇中所有选项及例子基于GNU find version 4.2.28) (一)Get Start 最简单的find用法莫过于如此: $ find . 查找当前目录下的所有文件.find命令的一般格式为: find [-H] [-L] [-P] [pa

re正则表达式13_review of regex symbols

Review of Regex Symbols This chapter covered a lot of notation, so here’s a quick review of what you learned: The ? matches zero or one of the preceding group. The * matches zero or more of the preceding group. The + matches one or more of the preced

Regex 手机号 座机 正则表达式

最近在工作中需要判断一个号码是否是手机号,是否是座机号. 在网上也搜到了大家总结的方法,没有直接使用这些方法是因为:手机号码在不断开始新的号码段(比如17x),座机号中个别区号由于行政区域的变化而废除. 这里的手机和座机的正则表达式基于本文作者目前找到的最新的手机号码段,区号(截止到2015年8月). 座机号码:http://baike.baidu.com/view/103379.htm. new Regex(@"^0?(10|(2|3[1,5,7]|4[1,5,7]|5[1,3,5,7]|7[

字符串切分 String.Split 和 Regex.Split

当切割字符串的是单个字符时可使用String.Split string strSample="ProductID:20150215,Categroy:Food,Price:15.00"; string[] sArray=strSample.Split(',');    //注意,这里用的是单引号,而非双引号 当切割字符串的是多个字符时只能使用Regex.Split string strSample="ProductID:20150215$_$Categroy:Food$_$P

groovy regex groups(groovy正则表达式组)

先看一个java正则表达式的例子. import java.util.regex.Matcher; import java.util.regex.Pattern; public class TestMatch { public static void main(String[] args) { Pattern pattern = Pattern.compile("G.*"); Matcher matcher = pattern.matcher("Groovy");

正则表达式练习 Regex Golf

1. foo 2. k$ 3. ^[a-f]*$ 4. (...).*\1 5. ^(?!.*(.)(.)\2\1) 否定环视即要求后方不出现匹配字符串 6. ^(.)(.).*\2\1$ 7. ^(?!(..+?)\1+$) 8. (.).\1.\1.\1 9. ^[a-e]*[f-z]*$ 正则表达式练习 Regex Golf,布布扣,bubuko.com

C++11 正则表达式库 (regex)

Source: http://cpprocks.com/wp-content/uploads/c++11-regex-cheatsheet.pdf C++11 正则表达式库 (regex),布布扣,bubuko.com