C++正则表达式例子
给了策划配置公式的地方,需要将策划配置的公式文本转化为可执行的脚本代码:
比如:self->mHp*2+target->2mMp*GetHit()+ self_mon->4mDan/1000 ==> self:lf_mHp(0)*2+dst:lf_mMp(2)*GetHit()+ src:lf_mDan(4)/1000
意思就是
1 指针变量凡是含有self的都变为src,target的都替换为dst
2 调用的属性前面的系数要提取出来
3 属性可能是多种多样的,所以提取方法不能写死。
当时时间紧急,对正则表达式也不熟,按照字符串分割的方法实现了。

代码如下:
void TestFormula(std::string& for_text)
{
using std::string;
using std::vector;
string::size_type pos = ;
string searchStr = "->";
string replaceStr = "#"; if (for_text.find(searchStr) == string::npos)
{
return;
} while ( (pos = for_text.find(searchStr, pos)) != string::npos)
{
if ((for_text.size() >= pos + && for_text[pos + ] == 'm') || (for_text.size() >= pos + && for_text[pos + ] == 'm' && for_text[pos + ] >= '' && for_text[pos + ] <= ''))
{
for_text.replace(pos, searchStr.size(), replaceStr);
}
pos++;
} vector<string> lVector;
vector<string> rVector;
int lastSplitIdx = ;
string lastText("");
for (string::size_type i = ; i < for_text.size(); i++)
{
if (for_text[i] == '+' || for_text[i] == '-' || for_text[i] == '*' || for_text[i] == '/'
|| for_text[i] == '#' || for_text[i] == '(' || for_text[i] == ')' || for_text[i] == ',')
{
if (for_text[i] == '#')
{
lastText = for_text.substr(lastSplitIdx, i - lastSplitIdx);
}
else
{
if (lastText.size() > )
{
lVector.push_back(lastText);
rVector.push_back(for_text.substr(lastSplitIdx, i - lastSplitIdx));
}
lastText = "";
}
lastSplitIdx = i + ;
}
}
if (lastSplitIdx != for_text.size())
{
if (lastText.size() > )
{
lVector.push_back(lastText);
rVector.push_back(for_text.substr(lastSplitIdx, lastText.size() - lastSplitIdx));
}
} if (lVector.size() == || rVector.size() == || lVector.size() != rVector.size())
{
return;
} pos = ;
int parseIdx = ;
while ( ( pos = for_text.find(replaceStr, pos)) != string::npos)
{
string leftStr = lVector.at(parseIdx);
string rightStr = rVector.at(parseIdx);
string oldStr = leftStr + replaceStr + rightStr;
string category = rightStr.substr(, );
string ower = leftStr;
if (category == "" || category == "" || category == "" || category == "" || category == "")
{
rightStr = rightStr.substr(, rightStr.size());
}
else
{
category = "";
}
if (leftStr.find("self", ) != string::npos)
{
ower = "src";
}
else if (leftStr.find("target", ) != string::npos)
{
ower = "dst";
}
string newStr = ower + ":lf_" + rightStr + "(" + category + ")";
for_text.replace(pos - leftStr.size(), oldStr.size(), newStr);
parseIdx++;
pos++;
}
}
int main()
{
std::string text("self->mHp*2+target->2mMp*GetHit()+self_mon->4mDan/1000");
std::cout << text.c_str() << std::endl;
TestFormula(text);
std::cout<<text.c_str()<<std::endl; return ;
}
运行结果:

颇费周折有木有~~~,还有很多临界状态需要检查,一不留神可能就踩到坑里了。
索性,看了看正则表达式,发现非常好用。代码少很多,结果一模一样。
void TestFormulaRegex(std::string& for_text)
{
using std::string;
std::regex pat("([A-Za-z_]+)->([0-4]*)(m[A-Za-z]+)");
string::const_iterator startPos = for_text.begin();
string::const_iterator endPos = for_text.end();
std::smatch mat;
while (std::regex_search(startPos, endPos, mat, pat))
{
string object(mat[].first, mat[].second);
string category(mat[].first, mat[].second);
string attrName(mat[].first, mat[].second);
if (category.compare("") == )
{
category = "";
}
if (object.find("self", ) != string::npos)
{
object = "src";
}
else if (object.find("target", ) != string::npos)
{
object = "dst";
}
string replaceStr(object + ":lf_" + attrName + "(" + category + ")");
for_text.replace(mat[].first, mat[].second, replaceStr);
startPos = for_text.begin();
endPos = for_text.end();
}
}
C++正则表达式例子的更多相关文章
- Perl正则表达式例子
Perl正则表达式 一.介绍 正则表达式各语言都有自己的规范,但是基本都差不多,都是由元字符的组合来进行匹配:由于Nmap内嵌的服务与版本探测是使用的Perl正则规范,因此此篇博客记录一下Perl正则 ...
- EmEditor正则表达式例子
正则表达式中 单词指的是由字母.数字.下划线组合而成的字符串,用符号表示为\w(小写). 空白符包括单字节空格.双字节空格.制表符,用符号表示为\s(小写). 1.匹配被双引号包含的所有字符串(str ...
- python正则表达式例子说明
pattern = re.compile('<div.*?author">.*?<a.*?<img.*?>(.*?)</a>.*?<div.* ...
- 关于Logstash中grok插件的正则表达式例子
一.前言 近期需要对Nginx产生的日志进行采集,问了下度娘,业内最著名的解决方案非ELK(Elasticsearch, Logstash, Kibana)莫属. Logstash负责采集日志,Ela ...
- 一个 Java 正则表达式例子
今天在项目里看到用 Python 正则表达式的时候,用到 group,没有仔细看.正好学习 Java 正则表达式,对 group 多留意了一下. 上代码: import java.util.regex ...
- java 正则表达式例子, 查找字符串
import java.util.regex.Matcher;import java.util.regex.Pattern; public class Main { public static voi ...
- pyqt 正则表达式例子学习
def rex01(self): username=QtCore.QRegExp('[a-zA-Z0-9_]{2,10}') self.names.setValidator(QtGui.QRegExp ...
- Java正则表达式例子汇总
1.过滤特殊字符 package com.sheepmu.text; /* * @author sheepmu */ public class HWCompetition { public stati ...
- notepad++正则表达式例子
1.匹配create table USR.APP ( 这样的字符串: create.*USR.APP\s+\(
随机推荐
- ACM 第九天
动态规划1 动态规划问题是面试题中的热门话题,如果要求一个问题的最优解(通常是最大值或者最小值),而且该问题能够分解成若干个子问题,并且小问题之间也存在重叠的子问题,则考虑采用动态规划. 1.LLS ...
- lintcode-138-子数组之和
138-子数组之和 给定一个整数数组,找到和为零的子数组.你的代码应该返回满足要求的子数组的起始位置和结束位置 注意事项 There is at least one subarray that it' ...
- DataSet和List 泛型之间互相转换 (转载)
//DataSet与泛型集合间的互相转换 //利用反射机制将DataTable的字段与自定义类型的公开属性互相赋值. //注意:从DataSet到IList<T>的转换,自定义类型的公开属 ...
- lol人物模型提取(五)
修改了发过去后,那边说吊坠的绳子太细了,厚度至少1mm,推荐是2mm,需要我自己加粗,没办法又得用3ds max一根一根线地缩放了. 修改好后问报价,高精度树脂打印需要730元,还不带上色的, ...
- Qt-排序
1.要求传入起始指针,总长度,单元素空间占用大小(sizeof(A[i])),判断函数. 判断函数参数类型为const void *,使用需要在函数内自行转换为对应类型, 返回值为整数型,升序排序时正 ...
- Combobox的使用,日期选择器
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- c++ int 负数 补码 隐式类型转换
unsigned y = ; ; cout << x + y << endl; 对于上述的结果为 这里面有一个负数的补码问题和不同类型之间的隐式类型转换问题 首先负数的表示方法 ...
- CURL & Fetch
CURL & Fetch https://kigiri.github.io/fetch/ https://stackoverflow.com/questions/31039629/conver ...
- matlab imwrite
函数功能:将图像数据写入到图像文件中,存储在磁盘上. 调用格式:imwrite(A,filename,fmt) A是图像数据,filename是目标图像名字,fmt是要生成的图片的格式. 图片格式有: ...
- hdu 1162 Eddy's picture (最小生成树)
Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...