C++ #include<regex>
https://www.cnblogs.com/coolcpp/p/cpp-regex.html
#include<iostream>
#include<regex>
using namespace std;
int main(){
regex reg("^(REGISTER|MESSAGE|BYE|INVITE|ACK|OPTIONS|PRACK|SUBSCRIBE|NOTIFY|PUBLISH|INFO|REFER|UPDATE)\\ssip:(?:(\\d*)@)?.*$");
cmatch m;
auto a="MESSAGE sip:34020000001320000003@10.19.201.32:5063 SIP/2.0";
auto ret=regex_match(a,m,reg);
if (ret){
cout<<m.str()<<endl;
cout<<m.length()<<endl;
cout<<m.position()<<endl;
}
cout<<111111111111<<endl;
for (auto i=0;i<m.size();i++){
cout<<m[i].str()<<" "<<m.str(i)<<endl;//两种方式都可以
}
}
Python import re
import re
regex= '^(REGISTER|MESSAGE|BYE|INVITE|ACK|OPTIONS|PRACK|SUBSCRIBE|NOTIFY|PUBLISH|INFO|REFER|UPDATE)\\ssip:(?:(\\d*)@)?.*$'
s='MESSAGE sip:34020000001320000003@10.19.201.32:5063 SIP/2.0'
Match=re.match(regex,s)
if Match:
print Match.groups()
else:
print 'no match'
s = '1102231990xxxxxxxx' #ID number
res = re.search('(?P<province>\d{3})(?P<city>\d{3})(?P<born_year>\d{4})',s)
print(res.groupdict())
C -lpcre正则表达式
https://blog.csdn.net/sulliy/article/details/6247155
#include <string.h>
#include <pcre.h>
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
#define MAX_SUBSTRINGS 30 /* should be a multiple of 3 */
int main(){
//char s[]=" req OPTIONS";
char REGEX[] = "^(REGISTER|MESSAGE|BYE|INVITE|ACK|OPTIONS|PRACK|SUBSCRIBE|NOTIFY|PUBLISH|INFO|REFER|UPDATE)\\ssip:(?:(\\d*)@)?.*$";
char s[]="MESSAGE sip:34020000001320000003@10.19.201.32:5063 SIP/2.0";
// char s[]="MESSAGE sip:10.19.201.32:5063 SIP/2.0";
pcre *re;
const char *error;
int erroffset;
int ovector[MAX_SUBSTRINGS];
int rc, i, res;
char arg[20], *ptr = arg;
re = pcre_compile(REGEX,0,&error,&erroffset,NULL);
if (re == NULL) {
printf("PCRE compilation failed at offset %d: %s\n", erroffset, error);
return 1;
}
rc = pcre_exec(re, // code, 输入参数,用pcre_compile编译好的正则表达结构的指针
NULL, // extra, 输入参数,用来向pcre_exec传一些额外的数据信息的结构的指针
s, // subject, 输入参数,要被用来匹配的字符串
strlen(s), // length, 输入参数, 要被用来匹配的字符串的指针
0, // startoffset, 输入参数,用来指定subject从什么位置开始被匹配的偏移量
0, // options, 输入参数, 用来指定匹配过程中的一些选项
ovector, // ovector, 输出参数,用来返回匹配位置偏移量的数组
MAX_SUBSTRINGS); // ovecsize, 输入参数, 用来返回匹配位置偏移量的数组的最大大小
// 返回值:匹配成功返回非负数,没有匹配返回负数
if (rc < 0) { //如果没有匹配,返回错误信息
if (rc == PCRE_ERROR_NOMATCH) printf("Sorry, no match ...\n");
else printf("Matching error %d\n", rc);
pcre_free(re);
return 1;
}
printf("\nOK, has matched ... ret is %d\n",rc); //没有出错,已经匹配
res = pcre_copy_substring(s, ovector, rc, 1, arg, 20); //20为arg缓冲区长度
if (res < 0) {
printf("pcre_get_substring 1 failed\n");
return 0;
}
printf("22222222222222222222222222222%s\n",ptr);
res = pcre_copy_substring(s, ovector, rc, 2, arg, MAX_SUBSTRINGS);
if (res < 0) {
printf("pcre_get_substring 2 failed\n");
return 0;
}
printf("22222222222222222222222222222%s\n",ptr);
}
IPV4地址的表达式
^((25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)$
局域网网段
(^10\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])$)|(^172\.(1[6789]|2[0-9]|3[01])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])$)|(^192\.168\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])$)