Jump to content

remedial regexp


subtledoctor

Recommended Posts

Sorry to ask about such a basic concept, I've been looking at regexp tutorials around the net but theya re all very in-depth, and I only need to know a very basic use: I want to do a REPLACE_TEXTUALLY for the first few chcracters plus everything else out to the end of the line.

 

Say, I want to replace the Stalker's HLA table with my own custom one. I could do something like

 

COPY_EXISTING ~luabbr.2da~ ~override~
   COUNT_2DA_ROWS ~2~ "rows"
   FOR ( row = 2 ; row < rows ; row = row + 1 ) BEGIN
      READ_2DA_ENTRY x y z
      PATCH_IF ( ~%entry%~ STRING_EQUAL_CASE ~STALKER~ ) BEGIN
         OUTER_SET this_row = %row%
      END
   END
   SET_2DA_ENTRY %this_row% 2 2 %new_table%
BUT_ONLY

But, that seems kind of overkill. With such a simple .2da file, it seems like it would be easier to simply do:

 

COPY_EXISTING ~luabbr.2da~ ~override~
   REPLACE_TEXTUALLY ~STALKER*~ ~STALKER %TAB% %new_table%~
BUT_ONLY

 

But that "STALKER*" should really be a regexp, which I'm not 100% sure of...

Link to comment

The most important thing here is to put bounds on what you're matching; you want to match STALKER but not NIGHTSTALKER or STALKER_VAR. The easiest way is simple to bound your match with spaces or tabs, e.g. [ %TAB%]STALKER[ %TAB%]. %TAB% is a special variable for WeiDU that matches the tab character. The brackets indicates that you want to match anything inside, almost like an or() statement; in this case you're matching a space or a tab.

 

In this case, though, STALKER leads the line, so you need a different match on the front end since there's not a space or tab in front of it. The caret is a special character which means the beginning of a line, so: ~^STALKER[ %TAB%]~. Now you've got a straight match for only STALKER. To replace the rest of the line, we just add the universal match-everything (a period) followed by a plus (to indicate there's going to one or more matches) and then a dollar sign to tell this to stop matching at the end of the line: ~^STALKER[ %TAB%].+$~. Another thing you can do to keep the original spacing is to match groups by wrapping stuff in escaped parentheses \( and \) to make groups:

REPLACE_TEXTUALLY ~^\(STALKER[ %TAB%]+\).+$~ ~\1%new_stuff%~

You can match groups and use them in the replacement text with \1, \2, \3, etc. (First group to be matched is \1, second \2, etc.) In this case, group 1 is STALKER and all of the spaces/tabs that follow it.

Link to comment

You can match groups and use them in the replacement text with \1, \2, \3, etc. (First group to be matched is \1, second \2, etc.) In this case, group 1 is STALKER and all of the spaces/tabs that follow it.

Interesting. Okay, now I'm going to complicate things, since I think the application has to be a bit more complicated. Maybe. This all starts from generating an array with certain values to find cleric kits:

COPY_EXISTING ~kitlist.2da~ ~override~
    COUNT_2DA_ROWS ~9~ "rows"
    FOR ( index = 2 ; index < rows ; index = index + 1 ) BEGIN
        READ_2DA_ENTRY %index% 5 9 clab
        READ_2DA_ENTRY %index% 1 9 name
        READ_2DA_ENTRY %index% 8 9 class
        DEFINE_ASSOCIATIVE_ARRAY d5_divine_kits BEGIN "%clab%" , "%name%" => "%class%" END
    END
BUT_ONLY

Now, I select only cleric kits to work on:

ACTION_PHP_EACH d5_divine_kits AS fox => sox BEGIN
  ACTION_IF (%sox% = 3) BEGIN //......clerics

Then it does some other stuff, adding spheres to the CLAB file (%fox%.2da) of each kit and editing the kit's description string to mention sphere access. It also creates a custom HLA table for this kit, using the first 6 characters of the CLAB file:

SNPRINT 6 ~luclab~ ~%fox%~
COPY ~f_n_p/spheres/hlas/luclxyz.2da~ ~override/lu%luclab%.2da~

Finally I want to edit LUABBR to make sure this kit uses its shiny new custom table:

COPY_EXISTING ~luabbr.2da~ ~override~
   REPLACE_TEXTUALLY ~^\(%fox_1%[ %TAB%]+\).+$~ ~\1%luclab%~
BUT_ONLY

So my question is, would the /1 work in this case, where the actual replacement text is in a variable and the routine is iterating through each instance? Or should I spell it out:

   REPLACE_TEXTUALLY ~^\(%fox_1%[ %TAB%]+\).+$~ ~%fox_1%+%TAB%+%luclab%~
Link to comment

\1 should work, yes. Though one more note even though you're not going to use this:

   REPLACE_TEXTUALLY ~^\(%fox_1%[ %TAB%]+\).+$~ ~%fox_1%+%TAB%+%luclab%~

Regexp special symbols--here + specifically--only have meaning when matching, not in the replacement. In this case, you'd end up with actual plus signs in luabbr.2da.

Link to comment

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...