Regex字符串在已分析字符串中的位置
Regex string location in parsed string
这将出现在一些伪代码中,因为我不确定如何完成其中的部分。
我正在尝试使用regex_iterator搜索字符串。但我想找到找到的子字符串的位置,而不是实际字符串本身。我希望我可以通过regex_iterator本身来实现这一点,而不必使用字符串::find类型函数。
file.open("file.mac", ios_base::in|ios_base::out);
if (!file) return 1;
char input_char;
string file_text;
string style_parse;
while (file >> noskipws >> input_char) {
file_text += input_char;
if (input_char == 'n') {
style_parse += 'n';
}
else {
style_parse += 'A';
}
}
regex re("([${]{2})([[:w:]]+)([}])");
sregex_iterator pos(file_text.begin(), file_text.end(), re);
sregex_iterator end;
// the location of search_text in file_text. Could also be a string
// iterator too
size_t string_location = 0;
// going to be pos->str(0) in the for loop;
string search_text;
for (; pos != end; ++pos) {
// Loop thru pos and get the location of each sub string. Use
// the location to replace values in style_parse. IE replace 'A'
// with 'B'
}
有没有办法通过迭代器做到这一点,或者我必须为找到的每个子字符串使用string::find?
我正在分析的文件如下所示。它基本上只是一个文本文件。
:Loop
/if ( ${MacroState.NotEqual[MAINLOOP]} ) {
/varset OldMacroState ${MacroState}
/varset MacroState MAINLOOP
}
/doevents
/if (${Me.Dead}) /call Wait4Rez
/if (!${EQBC.Connected}) /bccmd reconnect
|**/if (${Me.Invis}) /goto :Loop**|
/call AssistMA
/if (${DebuffList.Count[[]}) {
/if (${DoDebuff}) /call Debuff
/if (${Spawn[${MATarget}].PctHPs}<${WhenToBurn} && ${NameList.Find[${Spawn[${MATarget}].CleanName}]}) /call BurnName
/if (${DoHeal}) /call CheckGrp
/if (${DoCure}) /call CheckCureGrp
/call Mana
} else /if (${MobList.Count[[]} && !${DebuffList.Count[[]}) {
|**|/echo MobList: ${MobList} |-| DebuffList: ${DebuffList} -- ${DebuffList.Count[[]}**|
/if (${DoHeal}) /call CheckGrp
/if (${Spawn[${MATarget}].PctHPs}<${WhenToBurn} && ${NameList.Find[${Spawn[${MATarget}].CleanName}]}) /call BurnName
/if (${DoNuke}) /call Nuke
/if (${DoDoT}) /call DoT
/if (${DoPanther}) /call Panther
/if (${DoHeal}) /call CheckGrp
/if (${UsePet}) /call Pet
/if (${DoCure}) /call CheckCureGrp
/if (${DebuffedList.Count[[]}) /call CheckDebuffs
/call Mana
} else /if (!${MobList.Count[[]}) {
/if (${DoHeal}) /call CheckGrp
/if (${DoCure}) /call CheckCureGrp
/call Mana
/if (!${UnSlowedAdds} && !${Me.Casting.ID}) {
/if ( ${UsePet} ) /call CheckPetBuff
/if (${DoWild} && !${WildTimer}) /call CheckWild
/call CheckSelfBuff
/doevents
/if (${QueueCount}) /call DoBuffEvents
}
/if (${Defined[MobList]} && ${MobList.Length}) /deletevar MobList
/if (!${Defined[MobList]}) /declare MobList string outer
}
/if (${Cursor.ID}) /autoinventory
/if (${DoLoot} && ${Me.CombatState.NotEqual[COMBAT]}) /call LootMobs
/if (${Spawn[${MATarget}].Dead} || ${Spawn[${MATarget}].Type.Equal[NULL]}) /varset MATarget 0
/if (${DoLeash} && !${DoLeashToon}) /call Leash
/if (${DoLeashToon}) /call DoLeashPerson
/if (!${CheckExpTimer}) /call AutoAdjustExp
/call UpdateMobList
/goto :Loop
一如既往地感谢您的时间和帮助。
所以我终于弄明白了,这比我预期的要容易得多。喜欢有一个脑死亡的编码时刻。如果将来有人想做这样的事情,下面是for循环中的代码。
for (; pos != end; ++pos) {
smatch match = *pos;
string search_text = match.str();
size_t string_location = match.position();
//f(string) is just a lambda to pad out the proper amount of 'B'
string replace_text = f(search_text);
style_parse.replace(string_location, search_text.length(), replace_text);
}
相关文章:
- 如何将元素添加到向量的字符串位置
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 为什么字符串的 move() 会改变内存中底层数据的位置?
- 返回员工和工资(给定位置子字符串)
- 将文件复制到自定义位置,存在字符串转换问题
- 替换字符串位置 X 中的单词C++
- 在特定位置为字符串添加空格
- 计算在同一位置至少包含一个常用字符的不同字符串对
- 如何在另一个字符串的x位置插入一个字符串?
- 我如何知道字符串中字符相对于英文字母的位置值?
- C++ - 重置字符串流不会重置获取位置或清除标志吗?
- 反转字符串中单词的位置,而不更改 O(1) 空格限制中特殊字符的顺序
- 如何从字符串向量中选择第 n 个位置?
- 如何从输入字符串中提取特定位置的字符?
- C++字符串_Xout_of_range("无效的字符串位置");
- 将字符数复制到新字符串位置的字符串操作,例如 S[0]
- 如何从字符串位置构造CSTRING/STD :: String
- 如何在 boost::regex 中找到子匹配的字符串位置
- 空字符串位置返回为*
- 如果我将一个值赋给一个目前不使用的字符串位置,究竟会发生什么?