给了策划配置公式的地方,需要将策划配置的公式文本转化为可执行的脚本代码:
比如: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++正则表达式例子的更多相关文章

  1. Perl正则表达式例子

    Perl正则表达式 一.介绍 正则表达式各语言都有自己的规范,但是基本都差不多,都是由元字符的组合来进行匹配:由于Nmap内嵌的服务与版本探测是使用的Perl正则规范,因此此篇博客记录一下Perl正则 ...

  2. EmEditor正则表达式例子

    正则表达式中 单词指的是由字母.数字.下划线组合而成的字符串,用符号表示为\w(小写). 空白符包括单字节空格.双字节空格.制表符,用符号表示为\s(小写). 1.匹配被双引号包含的所有字符串(str ...

  3. python正则表达式例子说明

    pattern = re.compile('<div.*?author">.*?<a.*?<img.*?>(.*?)</a>.*?<div.* ...

  4. 关于Logstash中grok插件的正则表达式例子

    一.前言 近期需要对Nginx产生的日志进行采集,问了下度娘,业内最著名的解决方案非ELK(Elasticsearch, Logstash, Kibana)莫属. Logstash负责采集日志,Ela ...

  5. 一个 Java 正则表达式例子

    今天在项目里看到用 Python 正则表达式的时候,用到 group,没有仔细看.正好学习 Java 正则表达式,对 group 多留意了一下. 上代码: import java.util.regex ...

  6. java 正则表达式例子, 查找字符串

    import java.util.regex.Matcher;import java.util.regex.Pattern; public class Main { public static voi ...

  7. pyqt 正则表达式例子学习

    def rex01(self): username=QtCore.QRegExp('[a-zA-Z0-9_]{2,10}') self.names.setValidator(QtGui.QRegExp ...

  8. Java正则表达式例子汇总

    1.过滤特殊字符 package com.sheepmu.text; /* * @author sheepmu */ public class HWCompetition { public stati ...

  9. notepad++正则表达式例子

    1.匹配create table USR.APP (  这样的字符串: create.*USR.APP\s+\(

随机推荐

  1. hustoj题目标准xml格式

    具体格式可见google code. 分析了一下发现像<time_limit></time_limit><memory_limit></memory_limi ...

  2. LintCode-373.奇偶分割数组

    奇偶分割数组 分割一个整数数组,使得奇数在前偶数在后. 样例 给定 [1, 2, 3, 4],返回 [1, 3, 2, 4]. 挑战 在原数组中完成,不使用额外空间. 标签 数组 两根指针 code ...

  3. exception = {"元数据集合中已存在具有标识“xxx”的项。\r\n参数名: item"}

    vs提示:exception = {"元数据集合中已存在具有标识"xxx"的项.\r\n参数名: item"} 出现这个错误说明有重复的字段,有可能是继承的类里 ...

  4. djano modles values+ajax实现无页面刷新更新数据

    做项目的过程中想通过不刷新页面的方式来进行页面数据刷新,开始使用http://www.cnblogs.com/ianduin/p/7761400.html方式将查询结果数据进行序列化.发现可以行,但是 ...

  5. Jekyll 使用 Rouge 主题

    今日发现我的 Github Pages 中的代码并没有高亮,看了一下代码发现,原来的没有设置 css 样式的原因,我使用的代码高亮器是 rouge highlighter: rouge Rouge 是 ...

  6. canvas drawImage 不显示

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 用select模拟一个socket server

    1, 必须在非阻塞模式下,才能实现IO的多路复用,否则一个卡住就都卡住了.(单线程下的多路复用) 先检测自己,现在没有客户端连进来,所以会卡住. # 用select去模拟socket,实现单线程下的多 ...

  8. POJ3690:Constellations——题解

    http://poj.org/problem?id=3690 题目大意:给一个图和几个子图,判断有多少种子图在原图出现过. —————————————————————— 二维哈希即可,操作看代码,我觉 ...

  9. HDU3949:XOR——题解

    http://acm.hdu.edu.cn/showproblem.php?pid=3949 求n个数的异或和第k小. 参考:https://blog.sengxian.com/algorithms/ ...

  10. HDOJ.1009 FatMouse' Trade (贪心)

    FatMouse' Trade 点我挑战题目 题意分析 每组数据,给出有的猫粮m与房间数n,接着有n行,分别是这个房间存放的食物和所需要的猫粮.求这组数据能保证的最大的食物是多少? (可以不完全保证这 ...