English 中文(简体)
Grep for .PO file, or RegEx for a block of text between delimiters
原标题:

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

msgid "(*)"
msgstr "

...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.

问题回答

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...





相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签