2014-10-18 62 views
0

这将会出现在某些伪代码中,因为我不确定如何执行此操作的某些部分。解析字符串中的正则表达式字符串位置

我想要做的是使用regex_iterator搜索字符串。但我想找到找到的子字符串的位置在哪里,而不是实际的字符串本身。我希望我可以通过regex_iterator本身来做到这一点,而不必使用string :: 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 ::找到每个子串发现了什么?

我解析的文件看起来像这样。它基本上只是一个文本文件。

: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 

一如既往感谢您的时间和帮助。

回答

0

所以我终于明白了,这比我期待的要容易得多。爱有一个脑死的编码时刻。这里是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); 
} 
相关问题