Search code examples
regexreplacegreptextwrangler

Regex/grep replacement in TextWrangler, replacing subsequent matches with a specified text


What I need to happen

I'm a novice, sorry if the question has been answered, or is very confused, but I'm having a hard time searching since I don't know what this type of operation is called.

I'm using textwrangler to create cards for my Latin Anki deck. I have a pattern like this.

puell - a / puell - ae  
puell - am / puell - ās 
puell - ae / puell - ārum
puell - ae / puell - īs 
puell - ā / puell - īs  

I want to use this as a template, so I can copy it and change the relevant parts. In this case, I want to change every instance of puell to somewordstem, easy enough. Then I want to change every word(ending) after - to some specified word. For example (pseudo-latin):

stem - ae / stem - a
stem - ac / stem - ab
stem - ae / stem - orum
stem - ai / stem - ibus
stem - a / stem - ibus

I read in the helpfile and internet about subpatterns, but I don't want to insert a word that was already in the source, I want to insert a specified text to replace subsequent instances of each matched word.

How I'm doing it

I have this in the find-field (?P<stem>\w+)\ -\ (\w*) This finds the words. It also creates a subpattern named stem as well as numbered subpatterns. But what do I need to put in the replace field to get the pattern stem - end1 / stem - end2? That is, is it possible to use the subpatterns to replace (first occurrence of word:)\1 with (specified word:)a and \2 with b?

or do I have to write every line separately into the replace field, like this:

stem\ -a\ /\ stem\ -\ b 
stem\ -c\ /\ stem\ -\ b

Solution

  • I've read through your question multiple times, but I'm still not 100% sure what you're asking. If you're trying to get puell to be replaced with stem that should be relatively easy using the GUI find/replace function of TextWrangler (meaning use the basic find/replace without regex). However, if there are different stems (other words besides puell) that need to be changed to stem you could try using [ \w+ - ] and replacing it with [ stem -]. This will replace all [ puella - ] or any another word followed a space and a - with [ stem - ].
    To me, the endings don't seem to have a pattern to them (a --> ae, am --> ac, etc.) so I would consider doing the replacements one a time. I know it's not the most efficient, but unless you can provide me with some kind of pattern to replace them.

    Please add a comment if this is not what you were looking for.