views:

20

answers:

1

Start with this file:

msgid "a string"
msgstr ""

msgid ""
"A longer string wraps "
"on multiple lines."
msgstr ""

Grep RegEx to replace all msgstr lines like this:

msgid "a string"
msgstr "{a string}"

msgid ""
"A longer string wraps "
"on multiple lines."
msgstr ""
"{A longer string wraps "
"on multiple lines.}"

In my infinite naiveté, I would search for

\bmsgid "\b(*)\b"
msgstr "\b

...to replace it with

msgid "\1"
msgstr "{\1}"

But this doesn't get me anywhere. After searching the web and trying to adapt various solutions for Java or C#, I gave up and decided to ask here.

The file syntax you see above if for .PO translations files.

I am using a text editor for this, TextWrangler on Mac OS, which supports Grep RegEx syntax.

Thank you.

+1  A: 

There are two different patterns there. The first pattern of msgid "a string" seems to indicate that the total data following msgid is a one line pattern. The second pattern of `msgid "" indicates that the data follows in multiple lines.

Since they are two different patterns, and you will tie yourself in knots trying to match them all in one regex. (Unless you use Perl or something with some program logic in addition to the RegEx...) You said you wanted to use Text Wrangler so I will limit my comments to what works in that.

I think you will need to do two find / replace cycles with two saved patterns to do this easily in Text Wrangler (TW). In TW you can use the (?sm) flag at the beginning of your pattern to have .* patterns match line endings. TW saves the previous Grep matches so it is fairly easy once you get your two find / replace patterns that work.

So the one line pattern can be matched with the msgid "([^"]*)" pattern you have. The second pattern can be matched in BB Edit or TW with (?sm)msgid ""[^"]*(.*)msgstr ""

You potentially could write a regex with alteration and named captures to match the two patterns and replace in one go, but it would take a lot less time to write something quick in Perl...

drewk
Thanks Drew. It almost works, except the patter select everything from the first `msgid ""` to the last `msgstr""' with everything in between.
Try `(?sm)msgid ""[^"]*(.*?)msgstr ""`
drewk