Regex字符串在已分析字符串中的位置

Regex string location in parsed string

本文关键字:字符串 位置 Regex      更新时间:2023-10-16

这将出现在一些伪代码中,因为我不确定如何完成其中的部分。

我正在尝试使用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);
}