解析字符串中的正则表达式字符串位置

时间:2014-10-18 02:24:04

标签: c++ regex

这将是一些伪代码,因为我不确定如何做到这一点。

我要做的是使用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 :: 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

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

1 个答案:

答案 0 :(得分: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);
}